DYNAMIC VARIABLES PHP

technology , Posted by on 2014/12/27 13     Comments

Variable variable-- (1)

Dynamically create variables in PHP

This blog is to show how you can create variables in PHP dynamically.

Here are some sample codes:

This code will create five variable dynamically for you.

 

//create a initial variable for naming the dynamic variables

$var_name = “some_name_”;

 

//loop will generate the variable

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

{

$var_name .= $i ;

$$var_name = //do something ;

}

 

You will get five variables with name $some_name_1, $some_name_2, $some_name_3, $some_name_4 and $some_name_5.

 

Let me know if you have any problem understanding it.