[PHP] q: extract() and get_defined_vars()

2003-06-27 Thread Aric Caley
am I assuming correctly that get_defined_vars() returns an array pretty much
like the $GLOBALS array?

If I call extract() inside of a function, then the variables it creates will
be local to that function, correct?  OK, so, is there a way to get extract()
to define those variables as global, from within a function?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] q: extract() and get_defined_vars()

2003-06-27 Thread David Nicholson
Hello,


This is a reply to an e-mail that you wrote on Sat, 28 Jun 2003 at 01:41,
lines prefixed by '' were originally written by you.
 If I call extract() inside of a function, then the variables it
 creates will
 be local to that function, correct?  OK, so, is there a way to get
 extract()
 to define those variables as global, from within a function?

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 $var1BR /$var2;

The above code outputs testingBR /testing2.

All the best,

David.

--
phpmachine :: The quick and easy to use service providing you with
professionally developed PHP scripts :: http://www.phpmachine.com/

  Professional Web Development by David Nicholson
http://www.djnicholson.com/

QuizSender.com - How well do your friends actually know you?
 http://www.quizsender.com/
(developed entirely in PHP)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] q: extract() and get_defined_vars()

2003-06-27 Thread Aric Caley
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 $var1BR /$var2;

The above code outputs testingBR /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...



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] q: extract() and get_defined_vars()

2003-06-27 Thread David Nicholson
Hello,


This is a reply to an e-mail that you wrote on Sat, 28 Jun 2003 at 02:01,
lines prefixed by '' were originally written by you.
 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...

Yes, good point.  Also that avoids using variable variables which produces
much easier to read code.

David.

--
phpmachine :: The quick and easy to use service providing you with
professionally developed PHP scripts :: http://www.phpmachine.com/

  Professional Web Development by David Nicholson
http://www.djnicholson.com/

QuizSender.com - How well do your friends actually know you?
 http://www.quizsender.com/
(developed entirely in PHP)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php