note 53873 added to language.variables.scope
| From: | dpk at dpk dot net | Date: | Wed, 15 Jun 2005 17:25:57 +0000 |
| Subject: | note 53873 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90485@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53873
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53873&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53873&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53873&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53873&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53873&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53873&report=yes
Search -- http://master.php.net/manage/user-notes.php