Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate two variables to obtain one

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.

like image 505
Centurion Avatar asked Dec 03 '22 11:12

Centurion


2 Answers

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
like image 75
NikiC Avatar answered Dec 20 '22 10:12

NikiC


$i = 5;
$var[$i] = "eeee";
echo $var[$i];
like image 39
Your Common Sense Avatar answered Dec 20 '22 12:12

Your Common Sense