Php: What is the difference between global and $ GLOBALS []?

Source: Internet
Author: User
Global generates an alias variable pointing to the external variable of the function in the function, instead of the real external variable $ GLOBALS [] of the function. actually, the call is an external variable, and the internal and external variables of the function will always be consistent!

Global generates an alias variable pointing to the external variable of the function in the function, instead of the real external variable of the function.
$ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent!

Example
Function t1 (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Function t2 (){
$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];
}
$ Var1 = 5;
$ Var2 = $ var3 = 0;
T1 ();
Print $ var2. "\ n ";
T2 ();
Print $ var3. "\ n ";

The execution result is:
0
5

Why is one 0 and one 5 instead of two five?

Modify the example as follows:
Function t1 (){
Global $ var1;
$ Var1 = 2;
Unset ($ var1 );
}
Function t2 (){
$ GLOBALS ['var1'] = 3;
Unset ($ GLOBALS ['var1']);
}
$ Var1 = 1;
T1 ();
Print $ var1. "\ n ";
T2 ();
Print $ var1. "\ n ";

Enter 2 for the execution result;

1. $ GLOBALS is an array automatically formed by all defined global variables. The variable name is the index of the array. That is, $ GLOBALS ['var1'] is the same variable as the variable $ var1 outside the function. Therefore, after $ GLOBALS ['var1'] is deleted, the variable does not exist, all cannot be output.
Note: $ GLOBALS is an automatic global variable. This means it works in all scripts. You do not need to use global $ GLOBALS; to access a function or method.

2. global $ var1; is the alias variable "$ var1" generated outside the function $ var1. it is not a real external variable of the function and exists only inside the function, therefore, deleting an alias variable in a function does not affect the external variables, but you can modify the values of the external variables of the function.

Note: I still don't seem to understand this, but it seems that I can only understand it in this way, waiting for the guidance of senior people.

Global variables and $ GLOBALS global access variables

Maybe some people always want to know the difference between this or that: in the learning of php programs, including other programs, you can experiment on your own and add some ideas based on the results, sometimes it may be faster and more accurate than searching online. Next, let's take a look at how php accesses variables globally?
Example 1: global defines global variables.
Function test_global ()
{
Global $ var1;
$ Var1 = 'OK ';
Unset ($ var1 );
}
Test_global ();
$ Var2 = & $ var1;
Unset ($ var1 );
Echo $ var2;

Run the program without providing the result. Variables in the function can be accessed. As can be seen from the results, unset only disconnects the variable name from the variable value, and does not immediately destroy the value of the variable, and global variables defined in the function, actually, only the internal alias of the function is used externally, so we can still access $ var1 outside.

Example 2: $ GLOBALS accesses the variables defined outside the function within the function.
$ Waibu = 'out ';

$ Waibu = 'out ';
Function ff ()
{
Echo $ GLOBALS ['waibu'];
}
Ff ();
An error occurs when $ waibu is used directly in the function.

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.