Concatenate two variables to obtain one

11,468

Solution 1

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

Solution 2

$i = 5;
$var[$i] = "eeee";
echo $var[$i];
Share:
11,468
Centurion
Author by

Centurion

Programmer

Updated on June 27, 2022

Comments

  • Centurion
    Centurion almost 2 years

    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.