Page 1 of 1

Combine value and name of two variables

Posted: Sat Jan 01, 2005 9:13 am
by Kedo
Hi Folks!
I've a problem I can't get rid of.
First get the Code:

Code: Select all

$amount2 = 12345;
$no = 2;
Now I want to get the value of $amount<$no>, in this case the value of $amount2, thus "12345".

So, how can I make PHP understand that I want to combine the value and the name of the two variables and get the value of this new combined variable?

Thanks and regards,
Kedo

Posted: Sat Jan 01, 2005 9:16 am
by feyd
how do you want to combine the values? "$amount<$no>" doesn't work as a name as < and > are not part of valid identifiers.

Posted: Sat Jan 01, 2005 9:21 am
by Kedo
Yes, I know that this doesn't work, that's my problem.

I have some Variables, i.e. $var1, $var2 ... $var23, $var24 and so on, and I have another variable, i.e. $no = 5. Now I want to have the value of $var5, so I have to put some variables together to one variable name. $var + the value of $no as one variable name => $var5.

Posted: Sat Jan 01, 2005 9:23 am
by feyd
ah, :arrow: [devnet]metaprogramming[/devnet]

Posted: Sat Jan 01, 2005 9:27 am
by timvw

Code: Select all

$amount2 = 12345;
$no = 2;
 
echo ${'amount' . $no};
for more read: [php_man]variables.variable[/php_man]

Posted: Sat Jan 01, 2005 9:32 am
by Kedo
Thanks a lot, that's exactly what I've been looking for !