Re: Re: A variable inside a variable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux