In PHP, due to scope limitations, variable access restrictions are caused:
1. Global variables cannot be accessed in local scope
2. Local variables cannot be accessed in the global scope
For the first case, the following code will not work:
<? PHP // local scope (inside function) using global variables $a = 1; // Variables within the global scope function Test () { echo "a = {$a}<br/>"; accessing global scope variables in local scope } Test (); /* operation Result: notice:undefined variable:a in/users/lyq/mysite/index.php on line one a = */?>
So what do you do if you want to use global scope variables in a local scope?
Method One: Within a local scope, use the Global keyword to declare a variable with the same name as the global scope variable. Cause: After this declaration, the variables in the global scope and the variable with the same name in the local scope point to the same piece of memory. Let the code speak:
<?PHP//local scope (inside function) using global variables $a= 1;//variables in global scope (temporarily called "$ A $") functionTest () {/*In order to use the external $ A, the Global keyword is used here to declare a variable with the same name as "$a" (temporarily called "$ A $"), which points to the same piece of content, which can be used and modified within the local scope. */ Global $a;//if not declared, the external $ A will not be used properly Echo"A = {$a}<br/> ";//accessing global scope variables in a local scope $a= 100;//change the value of $ A by internal $ A
/*
Note: This does not destroy the variable, because there are already two references pointing to the content, so it is just a matter of breaking a reference
So, if you uncomment the code on the following line, the result is still normal.
*/
unset ($a);
} test (); Echo"The value of \ $a was modified inside the test function, at this time \ $a =$a"; /*Run Result: A = 1 The value of $ A is modified inside the test function, when $ A =*/?>
Method Two: Use $globals to access global variables.
<?PHP//local scope (inside function) using global variables $a= 1;//variables within the global scope functionTest () {/*use $globals to access the value of $ A: $GLOBALS use of global variables can be seen as another form of syntax for global variables rather than "reference" relationships*/ Echo"A = {$GLOBALS[' A ']} <br/> ";//accessing global action variables using $globals in a local scope $GLOBALS[' a '] = 100;//Modify the value of $ A by $globals[' a '] /*since $globals[' a '] is just another syntactic form of the global variable $ A, using $globals[' a '] does not create a new reference, so if the following code is called, the program will not function properly, output: a = 1 N otice:undefined variable:a in/users/lyq/mysite/index.php on line 21 a value of $ A was modified inside the test function, at which time $ A =*/ //unset ($GLOBALS [' a ']);} test (); Echo"The value of \ $a was modified inside the test function, at this time \ $a =$a"; /*Run Result: A = 1 The value of $ A is modified inside the test function, when $ A =*/?>
Attention:
By using the global variable $ A in the method, you are actually creating a new reference, so if you use unset ($a), you will not destroy the variable at this time. If you will approach a code of "//unset ($a);" After the annotation is run, the correct result can still be output. But $globals[' a '] is just another syntactic form of the global variable and does not create a new reference, so if you are in the annotation method two "//unset ($GLOBALS [' a ']);", the program will not function properly.
Accessing global variables within a local scope in PHP