note 53873 deleted from language.variables.scope by sean
| From: | sean@php.net | Date: | Wed, 15 Jun 2005 17:34:57 +0000 |
| Subject: | note 53873 deleted from language.variables.scope by sean | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-90486@lists.php.net to get a copy of this message | ||
Note Submitter: dpk at dpk dot net
Reason: bad code
----
Ah, OK. Well, this is still not what I would expect:
$GLOBALS['bar'] = 'frank';
var_dump ($bar);
function foo () {
var_dump ($GLOBALS['bar']);
var_dump ($bar);
}
foo ();
This'll print:
string(5) "frank"
string(5) "frank"
PHP Notice: Undefined variable: bar in /.../foo.php on line 6
NULL
So using $GLOBALS to set global variables to be used without the $GLOBALS name only works outside of
functions. You still have to use "global $bar" inside the function to use the variable as
just plain $bar.
So basically, there's no good reason whatever that $GLOBALS['bar'] should be
identical to $bar (in the global context), since the only time you really need the benefits of a
global variable is inside of functions, and it doesn't work there.