Analysis of super global variables for PHP $GLOBALS

Source: Internet
Author: User
Tags php code

PHP has a super global variable $globals that is not used by many people. The rational use of this variable can make the work more efficient, this article mainly analyzes the use of the Super global variable and the difference between $globals and global.

$GLOBALS definition: Refers to all the variables available in the global scope (a global combined array that contains all the variables). The name of the variable is the key of the array, and unlike all other hyper-global variables, $GLOBALS is always available anywhere in the PHP code, and you can tell by printing the result of the variable $globals.

In the PHP lifecycle, the so-called global variables defined outside the function body are not directly available within the function. If you want to access externally defined global variables in the body of a function, you can access them either through the global declaration or directly using $globals, for example:

$var 1= ' www.Alixixi.com ';

$var 2= ' www.google.cn ';

Test ();

function Test () {

$var 1= ' Taobao ';

echo $var 1, '

';

Global $var 1;

echo $var 1, '

';

echo $GLOBALS [' var2 '];

}

The results will be printed as:

Taobao

Www.Alixixi.com

www.google.cn

Here are the main differences between global and $globals:

$GLOBALS [' var '] is the external global variable itself, and the global $var is a reference or a pointer to an external $var, that is, the global creates an alias variable that points to the external variable of the function instead of the real function external variable, and $globals[] It is really called an external variable, and the function is always consistent inside and outside. The following examples illustrate:

$var 1=1;

$var 2=2;

function Test () {

$GLOBALS [' var2 ']=& $GLOBALS [' var1 '];

}

Test ();

echo $var 2;

Print result is 1

$var 1=1;

$var 2=2;

function Test () {

Global $var 1, $var 2;

$var 2=& $var 1;

}

Test ();

echo $var 2;

The print result is 2. Why print the result to 2? In fact, it's because the $VAR1 reference points to the $VAR2 reference address. Causes the value of the substance to not change. Let's look at one more example.

$var 1=1;

function Test () {

unset ($GLOBALS [' var1 ']);

}

Test ();

echo $var 1;

Because the $var1 was deleted, nothing was printed.

$var 1=1;

function Test () {

Global $var 1;

Unset ($var 1);

}

Test ();

echo $var 1;

The print result is 1. The deletion of the proof is only an alias reference, the value of its own has not been any change. In other words, global $var is $var=& $GLOBALS [' var ']. Call an alias of 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.