[Basic PHP knowledge] $ GLOBALS [] is different from global. In php program development, many developers did not notice the difference between $ GLOBALS [] and global. In fact, the difference between the two writing methods is quite different, not just literal, next I will understand the differences between $ GLOBALS [] and global in php program development. In fact, the differences between the two writing methods are quite different, not just literal differences, let me take a look at their specific differences.
Differences
1. $ GLOBALS ['var'] is the external global variable itself (actually External $ var itself ).
2. global $ var is an external $ var reference or pointer with the same name (it can be understood as an external $ var replacement ).
For example:
Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
$ Var2 = "test2 ";
Function test (){
$ GLOBALS ['var2'] = & $ GLOBALS ['var1'];
}
Test ();
Echo $ var2; // output test1
?>
The output result of the above code is test1.
Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
$ Var2 = "test2 ";
Function test (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Test ();
Echo $ var2; // output test2
?>
The output of the above code is a bit unexpected. The result is test2.
Why does test2 output? In fact, the reference of $ var1 points to the reference address of $ var2 (in general, $ var1 in the test function is a substitute ). The actual value is not changed.
Let's look at an example.
Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
Function test (){
Unset ($ GLOBALS ['var1']);
}
Test ();
Echo $ var1; // no output
?>
Because $ var1 has been actually deleted, nothing can be output.
Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
Function test (){
Global $ var1;
Unset ($ var1 );
}
Test ();
Echo $ var1; // output test1
?>
Test1 was unexpectedly output this time. It indicates that only the alias or reference (substitute) is deleted, and the value of the variable itself is not changed.
Do you understand?
In other words, global $ var is actually an alias for $ var = & $ GLOBALS ['var'] to call external variables.
...