RE: [PHP] Globals
| From: | Ford, Mike [LSS] | Date: | Mon, 04 Aug 2003 10:35:47 +0000 |
| Subject: | RE: [PHP] Globals | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-158135@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Chris Boget [mailto:chris@wild.net]
> Sent: 01 August 2003 20:18
>
> > I'm curious if someone could explain to me why this is occuring:
> >
> > function blah() {
> > //global $GLOBALS;
> > echo 'Globals: <pre>'; print_r( $GLOBALS ); echo '</pre>';
> >
> > }
> >
> > As it is shown above, with the 'global $GLOBALS' line commented
> > out, the print_r() works and shows all the currently
> defined variables
> > and their corresponding values. However, if I declare $GLOBALS
> > as global, nothing gets printed out.
> >
> > Why?
Well, I'm kinda guessing here, but it probably goes something like this:
global $GLOBALS;
is the equivalent of doing
$GLOBALS = &$GLOBALS['GLOBALS'];
but this causes $GLOBALS to be a local variable within the function, so the
sequence PHP executes probably goes like this:
(1) set up $GLOBALS as a local variable of the function, masking out the
true (super)global $GLOBALS.
(2) look up the 'GLOBALS' element of this new local $GLOBALS -- this is NULL
as no value has yet been assigned to the local $GLOBALS!
(3) create a reference to the NULL obtained from this lookup, and assign
that to the local $GLOBALS.
So what you've ended up in the $GLOBALS which is local to the function is a
reference to NULL, hence the behaviour you're seeing.
Like I say, this is all guesswork -- albeit slightly educated guesswork,
based on my scant and very ancient (over 20 years ago!) experience of
writing bits for a couple of interpreted languages. Maybe a PHP internal
person might come along and tell me how close I got...!! ;)
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211