收录日期:2020/12/05 22:50:31 时间:2010-09-07 20:29:21 标签:php,variables,concatenation

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.

You should consider using arrays instead, as those dynamic variables tend to only cause harm.

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

${'var' . $i} = 'eeee'; // sets $var5
${$var . $i} = 'eeee'; // sets $sss5
$i = 5;
$var[$i] = "eeee";
echo $var[$i];