Analysis of global and $globals[in PHP language

Source: Internet
Author: User
Analysis of global and $globals[in the PHP language
PHP in the global and $globals not only the wording is not the same, 2 of the difference is still very large, in practical applications need to pay attention to!


Let's look at the following example:

 
    


Reference

The result of the execution is:
0

5



How could this be? Shouldn't it be 2 5? How can there be 1 0 and a 5?


Well, we keep the above questions in-depth analysis of $globals and global principles!


We all know that variables are actually "code names" in the code of the corresponding physical existence.


$globals Explanation of PHP Manual reference:


Reference

Global variables: $GLOBALS, note: $GLOBALS are available in PHP 3.0.0 and later versions.


An array that consists of all defined global variables. The variable name is the index of the array. This is a "superglobal", or can be described as an automatic global variable.



In other words, $var1 and $globals[' var1 ' in the above code refer to the same variable, not 2 different variables!


What does global do now?


explanation of the global reference to the PHP manual:


Reference

If a variable declared as global is assigned to a reference within a function, the reference is only visible inside the function. You can avoid this by using an array of $GLOBALS.



We all know that the variables generated by the functions in PHP are private variables of the function, then the variables generated by the Global keyword will certainly not escape the rule, why do you say, look at the following code:

 
 
    


Reference

The result of the execution is:


1



Why does it output 1? Didn't you give $ A to unset? Unset failed? PHP bug?


is not, in fact unset work, is the test function of $ A to unset dropped, you can add in the function test ()


Print $a;

To test!

Then go back to the above example 1, see Test_global in the Code "$var 2 =& $var 1;", above is a reference assignment operation, that is, $VAR2 will point to the physical memory address pointed to by var1, so example 1 after the Test_global function has been executed , the variable changes only in the function of the local effect, the function outside the $var2 point of the physical memory address does not change, or it itself.


At this point, you can understand why example 1 after execution, $var 2 is 0, and $VAR3 is 5!


So we come to the conclusion that the difference between global and $globals[in a function is:


GlobalWhen a function produces an alias variable that points to an external variable of the function, rather than the actual function external variable, but changes the pointing address of the alias variable, some unexpected situations occur, such as Example 1.


$GLOBALS []The actual call is an external variable, and the function remains consistent inside and outside


The following two columns can be compared to deepen the impression:


Global


 
  


Output: someting

$GLOBALS []:

 
  


Output: Empty


When you follow the above understanding, the following situation is not a bit dizzy?
 
  


The output will be "3″." The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable.


Why not 2, outside the function is not not affected, note that $b in the function is not modified by reference, but the modified $b point to the value of physical memory, so 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.