Analysis of global and $ GLOBALS [] in PHP

Source: Internet
Author: User
The second analysis of global and $ GLOBALS [] in PHP. For more information, see the previous example:

PHP code
The code is as follows:
// Example 1
Function test_global (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Function test_globals (){
$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];
}
$ Var1 = 5;
$ Var2 = $ var3 = 0;
Test_global ();
Print $ var2. "\ n ";
Test_globals ();
Print $ var3. "\ n ";
?>

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.
$ 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?
Global interpretation of the reference php manual:
If a global variable is assigned to a reference within a function, the reference is only visible within the function. You can avoid this by using the $ GLOBALS array.
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 code is as follows:
// Example 2
Function test (){
Global $;
Unset ($ );
}
$ A = 1;
Test ();
Print $;
?>

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 to the test () function.
Print $;
To test!
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 will point to the physical memory address pointed to by var1. Therefore, after test_global function is executed in example 1, variable changes only have an effect on the local part of the function, external function $ var2 points to the physical memory address and does not change, or it itself.
Now, we can understand 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.
You can better understand the two columns below:
Global:
The code is as follows:
Function myfunction (){
Global $ bar;
Unset ($ bar );
}
$ Bar = "someting ";
Myfunction ();
Echo $ bar;
?>

Output: someting
$ GLOBALS []:
The code is as follows:
Function foo ()
{
Unset ($ GLOBALS ['bar']);
}
$ Bar = "something ";
Foo ();
Echo $ bar;
?>

Output: Null
After understanding the above ideas, is there any dizzy situation?
The code is as follows:
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

The output will be "3 ″. The global variables $ a and $ B are declared in the function. all referenced variables of any variable point to the global variable.
Why isn't it 2? isn't it not affected outside the function? please note that $ B does not reference and modify in the function, but the modified $ B points to the physical memory value, therefore, the external input is 3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.