Analysis of the difference between global and $global in PHP

Source: Internet
Author: User
Most people will think that global and $globals[] are just different, but it's not, so let's take a look at what's the difference between them.

According to the official explanation,

$GLOBALS [' var '] is the external global variable $var itself.

The global $var is a reference or pointer to an external $var with the same name. (Error: It is an individual name reference, non-pointer!!!) )

To illustrate:

Use of PHP $GAOBAL []:

    <?php    $var 1 = 1;    $var 2 = 2;    The    function test () {        $GLOBALS [' var2 '] = & $GLOBALS [' var1 '];    ), test ();    echo $var 2;    Ten    ?>

Normal print result is 1

Use of PHP Global:

    <?php    $var 1 = 1;    $var 2 = 2;    the function test () {        $var 1, $var 2;        $var 2 = & $var 1;        echo $var 2;        $var 2 = ' qianyunlai.com ';    ()//output 1    echo $var 2;//Output 2    echo $var 1;//Output Qianyunlai . com    ?>

The $VAR1 in the test () function, $va 2 are local variables, except that the global keyword is added, referring to $var1, $va 2, when $var 2 = & $var 1; , the local variable $var2 no longer points to the global variable $val2, and to the global variable $var1, in other words, the change of the local variable $var2 will no longer affect the global variable $val2, but will affect the re-directed global variable $VAL1.

Let's look at one more example.

1    <?php    2    $var 1 = 1;    3    function Test () {    4        unset ($GLOBALS [' var1 ']);    5    }    6    test ();    7    echo $var 1;    8    ?>

Because $var1 was removed, nothing was printed.

    <?php    $var 1 = 1;    Geneva    Function test () {        $var 1;        unset ($var 1);    ();    echo $var 1;    ?>

Accidentally printed 1.

Proof that the deletion is only the alias, $GLOBALS [' var '] reference, the value of itself has not been changed by any.

Do you understand?

This means that the global $var is actually $var = & $GLOBALS [' var ']. Call an alias of an external variable.

PHP in the global and $globals is not just the same way of thinking, the difference between the two is still very large, in practical applications need to pay attention to!

Let's look at the following example:

1    <?php    2    $id = 1;    3    function Test () {    4        global $id;    5        unset ($id);    6    }    7    test ();    8    Echo ($id);//Output 1    9    ?>

Reference positioning

Many of the syntax structures of PHP are implemented by reference mechanisms, so all of the above reference bindings apply to these constructs as well. Some structures, such as reference passing and reference return, have already been mentioned above. Other structures that use references are:

When you declare a variable with the global $var, you actually establish a reference to the global variable. In other words, it is the same as doing this:

    <?php    $GLOBALS ["var1"] = 1;    $var = & $GLOBALS ["var1"];    unset ($var);    echo $GLOBALS [' var1 '];//output 1    //############################################    $ globals["var1"] = 1;    $var = & $GLOBALS ["var1"];    unset ($GLOBALS [' var1 ']);    echo $var;//Output 1    //############################################    //If written as follows, an error will occur    $GLOBALS ["var"] = 1;    $var = & $GLOBALS ["var"];    unset ($GLOBALS [' var ']);    echo $var;//scripts cannot be executed    .    //###########################################    ?>

This means, for example, that the unset $var does not unset global variables.

Unset just breaks the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed.

Returns False when using Isset ($var). $this in the method of an object, $this is always a reference to the object that called it.

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.

Example to reference a global variable within a function:

    <?php    $var 1 = "Example variable";    $var 2 = "";    global_references function ($use _globals) {        $var 1, $var 2;        if (! $use _globals) {            $var 2 = & $var 1;//visible only inside the function, or        else {
  10            $GLOBALS ["var2"] = & $var 1;//visible also in global context    one        }    13    global_references (false);    echo "VAR2 is set to ' $var 2 ' \ n";//VAR2 is set to "    global_references (true);    "VAR2 is set to ' $var 2 ' \ n";//VAR2 is set to ' Example variable '    ?>

$var the global; As $var = & $GLOBALS [' var ']; The shorthand. So if you assign other references to $var, only the local variable references are changed.

As mentioned earlier, references are not pointers. This means that the following structure does not produce the expected effect:

1    <?php    2    $bar = 3;    3    function foo (& $var) {    4        $GLOBALS ["baz"] = 5;    5        $var = & $GLOBALS ["Baz"];    6    }    7    foo ($bar);    8    echo $bar;//Output 3    9    ?>

This causes the $var variables in the Foo function to be bound together with the $bar when the function is called, but is then re-bound to the $GLOBALS ["Baz"].

It is not possible to bind $bar to other variables within the scope of a function call by means of a reference mechanism, because there is no variable $bar in the function foo (it is represented as a $var, but $var only the contents of the variable without calling the name-to-value binding in the symbol table). You can use a reference return to refer to the variable selected by the function.

Refer to the PHP manual for $globals Explanation:

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!

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 function in PHP are private variables of the function, then the variables generated by the Global keyword will certainly not escape the rule, and global in the function produces a pointer to the function external variable alias variable, instead of the actual function external variable, one but changed the alias variable pointing address, There will be some unexpected situations, $GLOBALS [] The actual call is an external variable, and the function will always be consistent inside and outside.

    <?php    $a = 1;    $b = 2;    function Sum () {        $a, $b;        $b = $a + $b;    () Sum ();    echo $b;    Ten    ?>

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.

Related Article

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.