1. Accessing global variables within the global function
<? PHP $x=5; $y=10; function myTest () {global$x,$y; $y=$x+$y;} MyTest (); Echo $y // Output?>
If global is not used in the MyTest function, it cannot be used
PHP stores all global variables in an array called $GLOBALS [index]. Index holds the name of the variable. This array can be accessed inside the function, or it can be used to update global variables directly. such as $globals[' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y ']
2, Static when a function is complete, all its variables are usually deleted. However, there are times when you want a local variable not to be deleted.
function MyTest () {static $x =0;echo $x; $x + +;} MyTest (); MyTest (); myTest (); 012
Each time the function is called, the variable retains the value of the function before it was called. The variable is still a local variable of the function.
PHP Learning Log-Variable scope