In PHP, the analysis of global and $ GLOBALS [] was originally thought that both global and $ GLOBALS had the same syntax, but in actual application, we found that, there is a big difference between the two! Let's take a look at the following example: PHP code & lt ;? Php example 1functiontest_global () {global $ var1, $ var2; $ var2 analysis of global and $ GLOBALS [] in PHP []
In the past, we thought that both global and $ GLOBALS had the same way of writing, but in actual application, we found that the difference between the two is still very big!
Let's take a look at the following example:
PHP code
The execution result is:
0
5
How can this happen? Shouldn't it be 2 or 5? How can one 0 and one 5 appear?
Well, let's keep the above questions for an in-depth analysis of the principles of $ GLOBALS and global!
We all know that variables are actually the "code" in the corresponding physical code. suppose the memory allocated by the three variables we declared above is as follows:
$ GLOBALS reference to the php manual:
Global variable: $ GLOBALS
Note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.
An array composed of all defined global variables. The variable name is the index of the array.
This is a "superglobal", or it can be described as an automatic global variable.
That is to say, $ var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables!
Next, let's analyze what global has done?
We all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. why do we say this? let's look at the following code:
PHP code
The execution result is:
1
Why does output 1? Didn't $ a have been given to unset? Unset failed? Php bug?
None of them. in fact, the unset function works. it is used to drop $ a from the test function to the unset function. you can add it after the function.
Print $;
To test! That is to say, global generates the alias variable "$ a" for the external $ a function test. to be different from the external $ a variable, I make it -- test-> $, in this case, we can draw the following figure:
[No figure. thank you. you may be fooled]
Return to Example 1 above and check the code "$ var2 = & $ var1;" in test_global. the above is a reference value assignment operation, that is, $ var2 points to the physical memory address pointed to by var1, so after test_global function is executed in example 1, the variable changes can be seen from:
[No figure. thank you. you may be fooled]
After test_globals is executed, check the variable changes:
Now, we can see why $ var2 is 0 and $ var3 is 5 after Example 1 is executed!
So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:
Global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations may occur, example 1.
$ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent!