This article shares the differences between $globals and global variables in PHP.
First, global variable $globals
There are a lot of PHP global variables, such as the following are the Hyper global Variables (superglobal):
$GLOBALS, $_server,$_get,$_post,$_files,$_cookie,$_session,$_request,$_env.
Official Note: $GLOBALS-References all variables available in the global scope. A globally combined array that contains all the variables. The name of the variable is the key of the array. That is, the global variables that have occurred can be obtained by $globals this array.
In the PHP life cycle, the so-called global variables that are defined outside the function body cannot be obtained directly inside the function.
$foo = "Example content";Test ();
function Test () {
$foo = "local variable";
Echo ' $foo in current scope: '. $foo. '
";
Echo ' $foo in global scope: '. $GLOBALS ["foo"]. "
";
}
On the example above, to access the external $foo must use the $GLOBALS array. This is also true for external global variables that come in through the include file.
In PHP, global also has this function, which differs from the $globals in that the function produces an alias variable that points to the external variable of the function, rather than the actual external variable of the function. $GLOBALS [] The actual call is an external variable, and inside and outside the function is always consistent.
For a member variable in a class, the function in the class must be accessed using $this->, and cannot be used in $globals mode:
The role of global is to define globals, but this global variable is not applied to the entire site, but to the current page, including all files of include or require.
Second, the example explanation
function T1 () {
Global $var 1, $var 2;
$var 2=& $var 1;
}
Function T2 () {
$GLOBALS [' Var3 ']=& $GLOBALS [' var1 '];
}
$var 1=5;
$var 2= $var 3=0;
T1 ();
Print $var 2. " \ n ";
T2 ();
Print $var 3. " \ n ";
The result of the execution is:
0
5
Why not 2 5 instead of a 0 and a 5?
Revise the example again:
function T1 () {
Global $var 1;
$var 1=2;
Unset ($var 1);
}
Function T2 () {
$GLOBALS [' var1 ']=3;
unset ($GLOBALS [' var1 ']);
}
$var 1=1;
T1 ();
Print $var 1. " \ n ";
T2 ();
Print $var 1. " \ n ";
The execution result is entered only one 2;
1, $GLOBALS is an array that is 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, so when the $globals[' var1 ' is removed, the variable no longer exists and all cannot be output.
Note: $GLOBALS is an automatic global variable. This means that it works in all scripts. You do not need to use the global $GLOBALS in a function or method to access it.
2, "Global $var 1;" is the alias variable "$var 1" that produces the external $var1 of the function, which is not a true function external variable, he only exists inside the function, so even if the alias variable is deleted within the function, it does not affect the outside variable, but can modify the value of the function external variable.
Perhaps some people always want to know the difference between this or that: in the PHP program, including other programs in the study, self-experiment, according to the results of thinking, sometimes more than the Internet search may come faster, more accurate.
Let's take a look at the global scope of PHP access to variables to do?
Example one:
Global variables are defined.
function Test_global () {
Global $var 1;
$var 1= ' OK ';
Unset ($var 1);
}
Test_global ();
$var 2=& $var 1;
Unset ($var 1);
echo $var 2;
Do not give the results first, run the program yourself. Variables inside the function can be accessed.
As can be seen from the results, unset just disconnects the variable name from the value of the variable, does not immediately destroy the value of the variable, and the global variable defined inside the function, the actual external only uses the alias inside the function, so we can still access the $var1 outside.
Example two: $GLOBALS a variable defined outside the function to access the function.
$waibu = ' out ';
function ff () {
echo $GLOBALS [' Waibu '];
}
FF ();
Using $waibu directly inside a function can be an error.