In the development of PHP program, many developers did not notice the difference between $globals[] and global, these two kinds of writing is actually very different, not just the literal above differences, let me look at their specific differences.
Specific differences
1. $GLOBALS [' var '] is the external global variable itself (the actual external $var itself).
2.global $var is an external $var reference or pointer (which can be understood as an external $var alias).
To illustrate:
Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
$var 2 = "test2";
function Test () {
$GLOBALS [' var2 '] = & $GLOBALS [' var1 '];
}
Test ();
echo $var 2; The output test1
?>
The output of the above code is TEST1
Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
$var 2 = "test2";
function Test () {
Global $var 1, $var 2;
$var 2 = & $var 1;
}
Test ();
echo $var 2; The output test2
?>
The output of the above code is a bit unexpected, the result is test2
Why does it output test2? In fact, it is because $var1 refers to the $var2 of the reference address (popular in the test function of the $VAR1 is an alias). Causes the value of the substance to not change.
Let's look at one more example.
Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
function Test () {
unset ($GLOBALS [' var1 ']);
}
Test ();
echo $var 1; I can't output anything.
?>
Because the $var1 has been really deleted, so what things can not output.
Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
function Test () {
Global $var 1;
Unset ($var 1);
}
Test ();
echo $var 1; Output test1
?>
This time again unexpectedly output the test1. It proves that the deletion is only an alias or reference (a surrogate), and that the value of the variable itself is not changed.
Do you understand?
That is, the global $var is actually $var = & $GLOBALS [' var '] calls an alias of an external variable.
http://www.bkjia.com/PHPjc/363889.html www.bkjia.com true http://www.bkjia.com/PHPjc/363889.html techarticle in the development of PHP program, many developers did not notice the difference between $globals[] and global, these two kinds of writing is actually very different, not just the difference between the literal, below I come to understand ...