Set dynamically variables names


Look this method if you want to have variable variable names

Suppose we have to work with indeterminate variable's number.

If you have a $count as integer (e.g. $count=5) and you want to manage $a1, $a2,..., $a5 you can use the {} php wrapper to declare dynamically your variable's name.

This code

 

for ($i=1;$i<=$count;$i++)

{  

   $a{$i} = "something";

}

 

is the same as this

$a1 = "something";

$a2 = "something";

$a3 = "something";

$a4 = "something";

$a5 = "something";

 

Basically, you can set the number of variables you need!

 

Related articles