We can dynamically create a variable name based on the other variable value! It is a concept called PHP Variable variables. It might be useful at certain cases, lets see how we do it. So, It will be like this,

 

"A variable takes the value of a other variable and assumes it is the name of that variable"


For example, Check this, This is a normal variable assignment:
$foo='good';

Now, when the variable assigned with two dollar signs($), It takes the value of the variable and creates a new variable with that value.
$$foo='boy';
echo $foo $good; //outputs "good boy";

With this, we can dynamically create new variable like this,
<?php
$sd=array('jsu'=>'i','jsv'=>'like','jsw'=>'you');
foreach($sd as $key => $val)
	$$key=$val;
	
echo $jsu $jsv $jsw; //outputs "i like you"
?>

 


Comments (0)
Leave a Comment

loader Posting your comment...