Detailed explanation of the difference between $GLOBALS and global in PHP

  • 2021-12-04 09:34:12
  • OfStack

Both $GLOBALS and global in PHP can implement the function of global variables, so many people think that global and $GLOBALS [] are just the difference in writing, but they are not. GLOBALS is a super global variable, which is actually an array, while global is a keyword that declares a global variable!

1. Hyperglobal variable $GLOBALS

There are many PHP hyper-global variables, and the following are all 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 global composite array containing all variables. The name of the variable is the key of the array.

That is, the global variables that have appeared can be obtained through the array of $GLOBALS.

$GLOBALS ['var'] is the external global variable $var itself. global $var is the namesake reference or pointer to the external $var. (Error: It's an alias reference, not a pointer!!!)

In the life cycle of PHP, the so-called global variables defined outside the function body cannot be directly obtained inside the function.


$foo="Example content";
test();
function test(){
  $foo="local variable";
  echo '$foo in current scope: '.$foo."<br>";
  echo '$foo in global scope: '.$GLOBALS["foo"]."<br>";
}

As in the example above, the $GLOBALS array must be used to access the external $foo. It also applies to external global variables coming in through include files.

global in php also has this function, which is different from $GLOBALS in that:

global generates an alias variable in a function that points to a function external variable instead of a real function external variable.

$GLOBALS [] is really calling an external variable, and it will always keep 1 inside and outside the function.

For member variables in a class, functions in the class must use $this- > Can't be accessed in the way of $GLOBALS:

The role of global is to define a global variable, but this global variable does not apply to the entire Web site, but to the current page, including all files of include or require.

2. Example explanation


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 not 2 5s but 1 0 and 1 5? Modify the following example:


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 only 1 2 for the execution result;

1. $GLOBALS is an automatically formed array of 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 after deleting $GLOBALS ['var1'], the variable no longer exists and cannot be output.

Note: $GLOBALS is an automatic global variable. This means that it works in all scripts. You do not need to use global $GLOBALS in a function or method; To access it.

2. "global $var1; "Is the alias variable" $var1 "that produces the function external $var1. It is not a real function external variable, it only exists inside the function, so even if the alias variable is deleted inside the function, it will not affect the external variable, but the value of the function external variable can be modified.

Maybe some people always want to know the difference between this and that:

In the php program, including other programs in the study, do-it-yourself experiments, according to the results plus thinking, sometimes than the Internet search may come faster 1, more accurate 1. Let's talk about 1. What should php do to access variables in the global scope?

Example 1: global defines global variables.


function test_global(){
  global $var1;
  $var1='ok';
  unset($var1);
}
test_global();
$var2=&$var1;
unset($var1);
echo $var2;

Don't give the result first, run the program by yourself. The variables inside the function can be accessed. It can be seen from the results that unset only disconnects the variable name from the variable value, and does not destroy the variable value immediately. Moreover, the global variable defined inside the function actually only uses the alias inside the function, so we can still access $var1 outside.

Example 2: $GLOBALS accesses variables defined outside the function inside the function.


$codetc='out';
function ff(){
  echo $GLOBALS['codetc'];
}
ff();

Using $codetc directly inside the function will make an error.

Summarize


Related articles: