This article mainly introduces the difference between global and $GLOBALS[' '] in php. Interested friends can refer to it. I hope it will be helpful to everyone.
I always thought that there was no difference between global and $GLOBALS[' '] in php. I checked it today and found that there is a big difference between the two. I made the following summary:
global $var: It is a reference to the global variable $var;
$GLOBALS["var"]: It is the global variable $var itself, which is equivalent to $var.
Here are a few examples:
Example 1:
<?php $var1 = 1; $var2 = 2; function test() { $GLOBALS['var2'] = &$GLOBALS['var1']; } test(); echo $var2;//输出1 ?>
test() function $GLOBALS['var2'] is equivalent to the global variable $var1,
$GLOBALS['var2'] = $GLOBALS['var1'] functions to change $var2 is a reference to $var1, that is, $var2 is an alias of $var1, and both point to the same memory space, so the value of \$var2 becomes 1.
Example 2:
<?php $var1 = 1; $var2 = 2; function test(){ global $var1, $var2; $var2 = &$var1; echo $var2; $var2 = 'hello...'; } test(); // 输出 1 echo $var2; // 输出 2 echo $var1; // 输出 hello... ?>
In the test function, $var1 and $var2 are references (ie aliases) to the global variables $var1 and $var2 respectively
$var2 = &$var1; //The value of $var2 (local variable) in the test function is changed to the reference of $var1 in the function
At this time, the value of $var2 in the test function is equal to the value of $var1 in the function, which is also equal to the global variable The value of $var1, all three point to the same memory space. When the value of $var2 in the test function changes, the values of the other two ($var1 in the test function and $var1 in the global variable) also change. Variety.
Example 3.
<?php $var1 = 1; function test(){ unset($GLOBALS['var1']); } test(); echo $var1; ?>
As mentioned above, $GLOBALS['var1'] is equivalent to $var1 in global variables, unset($GLOBALS['var1' ] ); is equivalent to destroying the global variable $var1, so printing is empty
Supplement:
The unset() function in php is used to destroy variables. In many cases, it just destroys the variable, but in the memory The value is not destroyed (that is, the unset() function exponentially cuts off the relationship between the variable and the memory, destroys the variable name, the value in the memory is not destroyed, and the memory is not released). What needs to be noted is:
1. This function will only release the memory when the memory occupied by the variable exceeds 256 bytes.
2. The address will be released only when all variables pointing to the memory pointed to by the variable (such as all references to the variable) are destroyed.
Example 4.
<?php $var1 = 1; function test(){ global $var1; unset($var1); } test(); echo $var1; //结果为打印1 ?>
In this code, the variable defined using global in the test() function is actually just a reference to the global variable $var, which is destroyed in the test() function This variable is equivalent to destroying a reference to the global variable (a piece of memory has two names. Deleting one of the names will not affect the other name and the value of the memory). Therefore, when printing the global variable $var, the result is still 1. The operation of this code is similar to the following code:
<?php $var = 1; $var1 = &$var; unset($var1); echo $var; ?>
Look at another example of referencing global variables inside a function:
<?php $var1 = "我是变量var1的值"; $var2 = "我是变量var2的值"; function global_references($use_globals) { global $var1, $var2; if (!$use_globals) { $var2 = &$var1; echo $var1; echo $var2; echo "<br />"; } else { $GLOBALS["var2"] = &$var1; echo $var1; echo $var2; echo "<br />"; } } global_references(false); //1.打印:我是变量var1的值我是变量var1的值 echo $var1; echo $var2; echo "<br />"; //2.打印:我是变量var1的值我是变量var2的值 global_references(true); //3.打印:我是变量var1的值我是变量var2的值 echo $var1; echo $var2; echo "<br />"; //4.打印:我是变量var1的值我是变量var1的值 ?>
Because the parameters are false, so the statement in the if is executed, and the value of var2 declared in the global_references() function, which was originally a reference to the global variable var2, becomes a reference to var1, so the two variables printed in the global_references() function are both of the global variable var1. Quote.
1 The executed statement does not affect the value of the global variable, so the value declared at the beginning of the program is printed.
Because the parameter is true, the statement in else is executed to change the value of global variable var1 to the reference of global variable var1 (var1 declared in the global_references() function). This does not change the value of var2 declared in global_references() (it is still a reference to the original memory).
After 3, the global variable var2 has become a reference to the global variable var1, so the values of the two global variables are the same at this time.
Summary:
global $var: is a reference to the global variable $var;
$GLOBALS["var"]: is the global variable $var itself, that is Equivalent to $var.
If the former is a variable declared inside a function, its scope is the function, that is, it is only visible within the function. This variable is a reference to a global variable. Destroying the variable will not affect the function. The global variable it points to has an impact.
Related recommendations:
PHP reads external variable $GLOBALS
Cause of PHP json_encode($GLOBALS) error
The above is the detailed content of global and $GLOBALS[ in php. For more information, please follow other related articles on the PHP Chinese website!