Some different understandings of GLOBAL and $global[']

Source: Internet
Author: User
A global variable in PHP must be declared as global when used in a function (the keyword Global is defined in a function to be useful).

The role of global is to define globals, but this global variable is not applied to the entire site, but to the current page, including all files of include or require.

<? PHP $a = 123; function aa () {Global $a;//If a $ A is not defined as a Global variable, the body of the function cannot access a $ A outside the function body, but can define an identical name $a,//this variable is a local variable, which is equivalent to the local variable of C language. Can only be used inside the function body. echo $a; } AA ();?>

Global variables defined in the function body can be used outside the body, and global variables defined outside the body of the function cannot be used within the function body.

$global $a; $a = 123; function f () {echo $a;//Error,}//See the following example, function f () {global $a; $a =123;} f (); echo $a; Correct, you can use

Example comparison:

<?php $var 1 = 1; function test () {unset ($GLOBALS [' var1 ']);} test (); echo $var 1;?>

Because $var1 was removed, nothing was printed.

<?php $var 1 = 1; function test () {global $var 1; unset ($var 1);} test (); echo $var 1;?>

Accidentally printed 1. Proof that the deletion is only an alias reference, and that its value is not changed by any of its own.

The global $var is actually the & $GLOBALS [' var '], calling an alias of an external variable.
The $var1 and $globals[' var1 ' in the code above refer to the same variable, not two different variables.
PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, and some people may inadvertently change a global variable. In PHP, global variables must be globally declared as global when applied in a function.
PHP's global variable is defined as a global variable, but the global variable is not applied to the entire Web site, but is applied to the current page, including all files of include or require.

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 () {    global $a, $b;    $b = $a + $b;    }    Sum ();    echo $b;    ? >

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.

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

The global function produces an alias variable that points to the external variable of the function, not the actual external variable of the function, but changes the point address of the alias variable, something unexpected happens.

$GLOBALS [] The actual call is an external variable, and inside and outside the function will always be consistent!

  • 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.