Home > Article > Backend Development > [Basic knowledge of PHP] The difference between $GLOBALS[''] and global_PHP tutorial
In PHP program development, many developers do not notice the difference between $GLOBALS[] and global. The two ways of writing are actually very different, not just literal differences. Let me take a look. Their specific differences.
Specific differences
1.$GLOBALS['var'] is the external global variable itself (the actual external $var itself).
2.global $var is a reference or pointer of the same name as an external $var (can be understood as a substitute for an external $var).
Let’s give an example:
Copy to ClipboardQuoted content: [www.bkjia.com] $var1 = "test1";The output result of the above code is test1
Copy to ClipboardQuoted content: [www.bkjia.com] $var1 = "test1";The output of the above code is a bit unexpected, the result is test2
Why is test2 output? In fact, it is because the reference of $var1 points to the reference address of $var2 (in layman's terms, $var1 in the test function is a substitute). The resulting value of the substance has not changed.
Let’s look at another example.
Copy to ClipboardQuoted content: [www.bkjia.com] $var1 = "test1";Because $var1 has been truly deleted, nothing can be output.
Copy to ClipboardQuoted content: [www.bkjia.com] $var1 = "test1";This time test1 was unexpectedly output. It proves that only the alias or reference (substitute) is deleted, and the value of the variable itself is not changed in any way.
Understand?
In other words, global $var is actually an alias for $var = &$GLOBALS['var'] to call external variables.