Understanding the difference between $ GLOBALS and global in php

Source: Internet
Author: User
Tags php code

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

Difference between global and $ GLOBALS ['']

$ GLOBALS

$ GLOBALS-reference all available variables in the global scope
Description & para;

A global combination array containing all the variables. The variable name is the key of the array.

 

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:

<? Php
$ Var1 = 'www .111cn.net ';
$ Var2 = 'www .google.cn ';
Test ();
Function test (){
$ Var1 = 'Taobao ';
Echo $ var1, '<br/> ';
Global $ var1;
Echo $ var1, '<br/> ';
Echo $ GLOBALS ['var2'];
}


The result is printed as follows:

Taobao
Www.111cn.net
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.

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.