I read this article on php.net:
"When you declare a variable as global $var
you are in fact creating reference to a global variable. That means, this is the same as:
<?php
$var =& $GLOBALS["var"];
?>
This also means that unsetting $var
won't unset the global variable."
What I understand from this is that when you declare a variable as global $var
, you set another name for the variable name $GLOBALS["var"]. So if you echo one or another, you get the same thing. But I find the last sentence confusing, since the following snippet outputs 'red':
$GLOBALS['var']='blue';
global $var; //This is supposed to be equivalent to $var=& $GLOBALS["var"];
//echo $var,'<br/>';
$var='red';
echo $GLOBALS['var']; //I would expect this to output 'blue', instead of 'red'
I was expecting the output to be 'blue' since it states that unsetting $var
won't unset the global variable. Where're the mistake?
If I do unset($var)
, how should I check that the global variable was not unset?