Re: global $GLOBALS ??? 2nd...

From: Date: Thu, 12 Oct 2000 07:50:55 +0000
Subject: Re: global $GLOBALS ??? 2nd...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-19773@lists.php.net to get a copy of this message
Berthold Tenhumberg writes: > Is there a way to get all global variables in a function readable? > > For example: > > 1 function bla() { > 2 global $GLOBALS; > 3 echo $my_global_var; > 4 } > > Line 2 is not working... Why? > > -- > Berthold Tenhumberg > > (live long and prosper...) Line 2 is working--it's bringing $GLOBALS into the local scope--which basically does nothing, since $GLOBALS exists there automatically anyway. You'd need to do something along the lines of: function bla() { echo $GLOBALS['my_global_var']; } ...or... function bla() { globals $my_global_var; echo $my_global_var; } ...to get that one in there. For your original question, in PHP 4 you could get *all* global variables into the function namespace with extract(): function bla() { extract($GLOBALS); echo $my_global_var; } ...or, in previous versions, by looping: function bla() { reset($GLOBALS); while (list($key) = each($GLOBALS)) { global $$key; } echo $my_global_var; } Hope this helps, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#19773) next »