Re: q: extract() and get_defined_vars()

From: Date: Sat, 28 Jun 2003 01:00:40 +0000
Subject: Re: q: extract() and get_defined_vars()
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-153237@lists.php.net to get a copy of this message
>Yes it is, you must tell the function to treat the relevant variables as >globals though, here is a way of doing it: >function Foo(){ > $theArray = array('var1'=>'testing', > 'var2'=>'testing2'); > foreach($theArray as $varname=>$value){ > global $$varname; > } > extract($theArray); >} > >Foo(); >echo "$var1<BR />$var2"; > >The above code outputs "testing<BR />testing2". Pretty sneaky. But at this point, why bother with the extract() at all? wouldn't this do the same thing: function Foo(){ $theArray = array('var1'=>'testing', 'var2'=>'testing2'); foreach($theArray as $varname=>$value){ $GLOBALS[$varname] = $value; } } Which is what I am already doing...

« previous php.general (#153237) next »