Php $ GLOBALS Super global variable analysis _ PHP Tutorial

Source: Internet
Author: User
Php $ GLOBALS Super global variable analysis. In php, there is a super global variable $ GLOBALS that is not used by many people. Reasonable use of this variable can make work more efficient. This article mainly analyzes the Super global variable $ GLOBALS which is not used by many people in php. Reasonable use of this variable can make work more efficient. This article mainly analyzes the usage of this super global variable and the difference between $ GLOBALS and global.

$ GLOBALS definition: reference all variables available in the global scope (a global combination array containing all variables. The variable name is the key of the array. unlike all other super-global variables, $ GLOBALS is always available anywhere in the PHP code, you can print the result of $ GLOBALS.

In the PHP lifecycle, the so-called global variables defined outside the function body cannot be directly obtained within the function. If you want to access the externally defined global variables in the function body, you can use the global declaration or directly use $ GLOBALS for access. for example:

 ';global $var1;echo $var1,'
'; echo $GLOBALS['var2'];}

The result is printed as follows:

Taobao
Www.phpernote.com
Www.google.cn

The following describes the differences between global and $ GLOBALS:

$ GLOBALS ['var'] is an external global variable, while global $ var is an external reference or pointer with the same name, that is to say, global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function, while $ GLOBALS [] actually calls the external variable, the internal and external functions are always consistent. The following is an example:

$var1=1;$var2=2;function test(){$GLOBALS['var2']=&$GLOBALS['var1'];}  test();echo $var2;

The printed result is 1.

$var1=1;$var2=2;function test(){global $var1,$var2;$var2=&$var1;}  test();echo $var2;

The printed result is 2. Why is the result 2 printed? In fact, it is because the $ var1 reference points to the reference address of $ var2. The actual value is not changed. Let's look at an example.

$var1=1;function test(){unset($GLOBALS['var1']);}  test();echo $var1;

Because $ var1 is deleted, nothing is printed.

$var1=1;function test(){global $var1;unset($var1);}  test();echo $var1;

The print result is 1. It indicates that only the alias is deleted. | the referenced value is not changed. That is to say, global $ var is actually $ var = & $ GLOBALS ['var']. Call an alias of an external variable.

Articles you may be interested in
  • Summary of common system variables in Thinkphp templates
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Common PHP variable judgment functions
  • Differences between variables and functions in php after the static keyword is added
  • PHP uses Curl Functions to Capture webpages and download files with multiple threads
  • Php built-in variable DIRECTORY_SEPARATOR parsing
  • Php output yesterday, today, tomorrow is the day of the week
  • Summary of system constants in the Action Controller of thinkphp

Bytes. Reasonable use of this variable can make work more efficient. This article mainly analyzes the use of this super global 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.