views:

39

answers:

2

Hi all, How to concatenate two variables to obtain something like this?

$var = "sss";
$i = 5;
${$var.$i} = "eeee"; // I know this is not correct, What should be here
echo $var5;

So here i need to obtain variables $var1 $var2 $var3 $var4 ... dynamically.

+4  A: 

You shall use an array instead. These dynamic variables will only cause harm.

But basically what you do is syntactically correct, it should work.

${'var' . $i} = 'eeee'; // sets $var5
${$var . $i} = 'eeee'; // sets $sss5
nikic
Oh thanks , I was messing up some thing but your comments made thing clear.
Centurion
@Centurian: No prob ;) @Downvoter: Could you elaborate why exactly you downvoted this?
nikic
+2  A: 
$i = 5;
$var[$i] = "eeee";
echo $var[$i];
Col. Shrapnel