Re: Re: A variable inside a variable?
| From: | tg-php at gryffyndevelopment dot com | Date: | Mon, 26 Jun 2006 19:24:02 +0000 |
| Subject: | Re: Re: A variable inside a variable? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-238662@lists.php.net to get a copy of this message | ||
You were on the right track, but this isn't going to work.. for a couple reasons:
$var = 1; # this is fine
$var2 = "$var"; # $var2 == 1 at this point
echo $$var2; # you're going to echo $1
Putting $var in double quotes makes PHP evaluate it before assigning it to $var2, so you won't
get $var but the value of $var (1).
If you did use single quotes, you'd get this:
$var = 1; # this is fine
$var2 = '$var'; # $var2 == '$var' (literal)
echo $$var2; # you're going to echo $$var
It seems like you might get $var2 evaluate to $var.. then with $$var have it evaluate to 1, but
doesn't look like PHP digs that deeply. When I ran it, I got NULL back.
This might be what you were aiming for:
$var = 1; # this is fine
$var2 = 'var'; # remove the $.. then you can use single or double quotes
echo $$var2; # $var == var, $var == 1, this should output correctly
Good lesson in 'gotchas' though.
-TG
= = = Original message = = =
$var=1;
$var2="$var";
echo $$var2;
It~ll echo the $var~s value.
Hope it~ll help you.
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.