Global and $ GLOBALS []

Source: Internet
Author: User
Global variables and $ GLOBALS [] are used for PHP development projects. global variables are inevitably used, such as the configuration information of some websites, you can set it in one place and call it in multiple places. You can define a variable as a global variable in two ways: global and $ GLOBALS []. Global and $ GLOBALS [] are used for many global variables in PHP.

When using PHP to develop projects, global variables are inevitably used. for example, some website configuration information can be set in one place and called in multiple places. You can define a variable as a global variable in two ways: global and $ GLOBALS []. Many people think that global and $ GLOBALS [] are different in writing, but they are actually different.

Let's take a look at global.
Php parses global variables as follows: Global defines global variables, but this Global variable is not applied to the entire website, but to the current page, including all files of include or require.

Let's look at the following PHP code:

$ A = 123; function test1 () {global $ a; // if $ a is not defined as a global variable, the echo $ a of $ a cannot be accessed in the function body; // 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 that the global variables defined in the function body can be used in vitro, and the global variables defined in vitro cannot be used in the function body.

Let's take a look at $ GLOBALS [].

$var1 = 1;  $var2 = 2;  function test1(){       $GLOBALS['var2'] = &$GLOBALS['var1'];  }  test1();  echo $var2;  //1$var3 = 1;  $var4 = 2;  function test2(){       global $var3,$var4;       $var4 = &$var3;  }  test2();  echo $var4;   //2

? Why is the printing result of $ var2 1 and that of $ var4 2? In fact, it is because the reference of $ var3 points to the reference address of $ var4. The actual value of $ var4 is not changed. The official explanation is: $ GLOBALS ['var'] is the external global variable itself, and global $ var is the reference or pointer of the external $ var with the same name.

Maybe this example is not very clear. I will introduce another example:

$var1 = 1;  function test1(){       unset($GLOBALS['var1']);  }  test1();  var_dump($var1);   //NULL$var2 = 1;  function test2(){      global  $var2;       unset($var2);  }  test2();  echo $var2;   //1

? The value of $ var1 is deleted, and the value of $ var2 still exists. This proves that $ var2 is just an alias reference and its value has not been changed. That is to say, global $ var is actually $ var = & $ GLOBALS ['var'], calling an alias for an external variable!

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.