The difference between the global and PHP's $ GLOBALS

global keyword, usually added before the variables, you can make the variable scope is global.

$ GLOBALS Predefined superglobals, the variables thrown inside and they can become global variables.
$ GLOBALS is an associative array, each element of a variable, the variable name corresponds to the name of the key, the value corresponding to the contents of the variable. $ GLOBALS exists in any global in scope, is because $ GLOBALS is a superglobal.

global $ var: refers to the function of the external reference to a variable of the same name

$ GLOBALS [ 'var']: refers to the function of the external variable itself

$a = 100;
function one(){
    global $a;
    unset($a);
}
one();
echo $a;
// 输出 100
/*******************************/
$a = 100;
function two(){
    unset($GLOBALS['a']);
}
two();
echo $a;
// 输出 Notice: Undefined variable: a

global $ var; equivalent to $ var = & $ GLOBALS [ 'var'];

The release of a global variable within a function, it should be like this:

unset($GLOBALS['var']);

But not like this:

global $var; unset($var);

Guess you like

Origin www.cnblogs.com/chenyuphp/p/11746978.html