Difference between global and $ GLOBALS [""] in php

Source: Internet
Author: User
In php, the difference between global and $ GLOBALS [& quot;] was originally found that both global and $ GLOBALS have the same syntax, however, in actual use, it is found that the difference between the two is still very big! Let's take a look at the following example: PHP code & lt ;? PHP // example 1functiontest_global () {... "/> <SC

In php, the difference between global and $ GLOBALS [""] originally felt that both global and $ GLOBALS had the same writing style, however, in actual use, it is found that the difference between the two is still very big!

Let's take a look at the following example:
PHP code

  // Example 1
Function test_global (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Function test_globals (){
$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];
}
$ Var1 = 5;
$ Var2 = $ var3 = 0;
Test_global ();
Print $ var2. "n ";
Test_globals ();
Print $ var3. "n ";
?>

Performance:
0
5

How can this happen? Shouldn't it be two or five? How can one 0 and one 5 appear?

Well, let's save the above questions and analyze the principles of $ GLOBALS and global in depth!

We all know that variables are actually the "code" in the corresponding physical code ",

$ GLOBALS reference to the PHP manual:

Global variable: $ GLOBALS
Note: $ GLOBALS is used in PHP 3.0.0 and later versions.

An array composed of all defined global variables. The variable name is the index of the array.

This is a "superglobal" or can be depicted as an active global variable.
That is to say, $ var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables!

Next, let's analyze what global has done?

We all know that all the variables produced by functions in PHP are private variables of the function, so the variables produced by global cannot escape this rule. why do we say this? let's look at the following code:
PHP code

 

Function test (){
Global $;
Unset ($ );
}
 
$ A = 1;
Test ();
Print $;
?>

Copy code
Performance:
1
Why does output 1? Didn't $ a have been given to unset? Unset failed? PHP bug?

None of them. in fact, the unset function works. it is used to drop $ a from the test function to the unset function. you can participate in it after the function.
Print $;
That is to say, global generates the alias variable "$ a" for the external $ a of the test function, in order to be different from the external $

Return to Example 1 above and check the code "$ var2 = & $ var1;" in test_global. the above is a reference value assignment operation, that is, $ var2 points to the physical memory address pointed to by var1.

So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:

Global generates an alias variable in the function that points to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations will occur, example 1.

$ GLOBALS [] the actual call is an external variable, and the internal and external functions will always be consistent!

You can compare the following two columns:

Global:

Function myfunction (){
Global $ bar;
Unset ($ bar );
}
$ Bar = "someting ";
Myfunction ();
Echo $ bar;
?>

Output: someting

$ Global []:

Function foo ()
{
Unset ($ GLOBALS ['bar']);
}

$ Bar = "something ";
Foo ();
Echo $ bar;
?>

Output: Null

Global variables in PHP are a little different from those in C language. in C language, global variables take effect in functions unless they are enveloped by local variables. This may cause some problems. some may carelessly change a global variable. Global variables in PHP must be declared as global when used in functions.
Example of applying global
$ A = 1;
$ B = 2;

Function Sum ()
{
Global $ a, $ B;

$ B = $ a + $ B;
}

Sum ();
Echo $ B;
?>
The output of the above script is "3 ". The global variables $ a and $ B are declared in the function. all referenced variables of any variable point to the 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.