Combine value and name of two variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kedo
Forum Newbie
Posts: 6
Joined: Tue Nov 30, 2004 12:04 am
Location: Cologne, Germany
Contact:

Combine value and name of two variables

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Kedo
Forum Newbie
Posts: 6
Joined: Tue Nov 30, 2004 12:04 am
Location: Cologne, Germany
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ah, :arrow: [devnet]metaprogramming[/devnet]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$amount2 = 12345;
$no = 2;
 
echo ${'amount' . $no};
for more read: [php_man]variables.variable[/php_man]
Kedo
Forum Newbie
Posts: 6
Joined: Tue Nov 30, 2004 12:04 am
Location: Cologne, Germany
Contact:

Post by Kedo »

Thanks a lot, that's exactly what I've been looking for !
Post Reply