Application of global variables in PHP Global and $globals[]

Source: Internet
Author: User
Use global variables in PHP and $globals[]

In PHP development projects, will inevitably use the global variables, such as some Web site configuration information, the whole station general, it can be set in one place, and then multiple places to call. There are two ways to define variables as global variables: Global and $globals[]. A lot of people think that global and $globals[] are just different, but there are differences.

Let's take a look at global
PHP's resolution to the global variable is that global variables are defined globally, but this global variable is not applied to the entire site, but is applied to the current page, including all files of the include or require.

Check out the following PHP code:

$a =123;function test1 () {global $a;   If $ A is not defined as a global variable, the function body is not able to access a $ A echo $a; 123}test1 (); global $b; $b =456;function test2 () {var_dump ($b);    Null}test2 (); function test3 () {global $c; $c = 789;} Test3 (); Echo $c;   789

? The code concludes: Global variables defined in the function body can be used outside the body, and global variables defined outside the function body cannot be used within the function body.

Take a look at $globals[]

$var 1 = 1;  $var 2 = 2;  function Test1 () {       $GLOBALS [' var2 '] = & $GLOBALS [' var1 '];  }  Test1 ();  echo $var 2;  1$VAR3 = 1;  $var 4 = 2;  function Test2 () {       Global $var 3, $var 4;       $var 4 = & $var 3;  }  Test2 ();  echo $var 4;   2

Why is the $VAR2 printing result is 1, and the $VAR4 print result is 2? This is because $VAR3 's reference points to the $VAR4 's reference address. The actual value of $var 4 has not changed. The official explanation is that $GLOBALS [' var '] is the external global variable itself, and the global $var is an external $var reference or pointer to the same name.

Perhaps this example is not very clear, I will introduce another example:

$var 1 = 1;  function Test1 () {       unset ($GLOBALS [' var1 ']);  }  Test1 ();  Var_dump ($var 1);   NULL$VAR2 = 1;  function Test2 () {      global  $var 2;       Unset ($var 2);  }  Test2 ();  echo $var 2;   1

? The value of $var 1 is deleted, and the value of $VAR2 is still present. This proves that $var 2 is only an alias reference, and the value of itself is not changed by any means. This means that the global $var is actually $var = & $GLOBALS [' var '], calling an alias of an external variable!

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