For a novice in the actual operation
But in the actual operation, will encounter a few times the global variable invalid depressed things, the following focus on the PHP global variables can not be effective reasons and solutions.
1.PHP global variable does not take effect error recurrence
The problem arises when using my simple framework, when you use the original third-party defined function in view (this function is more independent), the following simulation:
t1.php
- < ?
- Run (); Perform
- function Run () {
- Include ' func.php ';
- ShowGlobal ();
- }
- ?>
Func.php1
- < ?
- $ VARs ' I am global! ' ;
- function ShowGlobal () {
- Global $vars;
- Print (' I use global variables: '. $vars);
- }
- ?>
Very simple two files (the first time when the problem, far more complicated than this, after a layer of error, to get the least of the problem to reproduce the environment), func.php is a well-defined third-party function, the function uses some global variables, if you put these two files together, perform a t1.php, Will find the ShowGlobal in the $vars is not visible, global failure?
2.PHP global variable does not take effect error reason
After searching, it was found that some people had suggested it earlier, and some explanations were given: php.net
Originally, when included func.php in the run function of t1.php, the scope of the variable $vars in func.php was only within run, while in ShowGlobal using the global declaration of $ VARs is required to belong to t1.php is not a run function, so it is empty)
3.PHP global variable does not take effect workaround
Knowing the reason, the solution is very simple, you can remove the include from run, so that the $vars in the func.php is t1.php, or you can declare $vars in run with global, so you can also declare the $vars of the original run as the global (belonging to t1.php);
Although the problem can be simple, but it is still very uncomfortable to use, because in my phpec framework, the case of include is more common, it is not possible to move the view on-demand include to the outer layer, using global, when I use a third-party function, And don't want to know what global variables it uses, and, more hierarchies, ....
Summary of reasons why PHP global variables are not valid:
1) Minimize the multi-level and function in the include file.
2) Try not to use global variables
http://www.bkjia.com/PHPjc/446107.html www.bkjia.com true http://www.bkjia.com/PHPjc/446107.html techarticle for a novice in the actual operation, but in the actual operation, will encounter several times the global variable is not valid depressed thing, the following focus on the PHP global variable can not be effective reason and solve ...