variable problem

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
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

variable problem

Post by MedaXis »

hi everyone

I'd like to create a variable like this:

$i = 5;
["var_" + $i] = "this is the value";

where ["var_" + $i] will be the variable name (in this example that would be var_5
how can I accomplish this?

Steve
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

the concatenation operator is the period

Post by lostboy »

so to do what you want, do this

$i = 5;
$v="var_";
$v.$i //will gove the result of var_5

hth
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

To add to what lostboy said, to use the new name as a variable, you need to enclose it in curly braces:

Code: Select all

<?php
$i = 5 ;
${"var_" . $i} = "I am $var_5" ;
?>
The PHP manual on variable variables describes this.
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

Post by MedaXis »

thanks to both of you!

Steve
Post Reply