<?php $str_1="test1"; $strt_2="test2";
for($i=1; $i<3; $i++) { $res="$"."str_$i"; echo $res. "<br>"; } ?>
out put...
$str_1 $str_1
i am trying get the value of $str_1 and str_2, somereason i could not get the result. Can anyone please hlep me for this codning?
Thanks
Regards Raj
You can't do it that way. You might want to consider using an array.
$vars = array("test1", "test2"); foreach ($vars as $var) { echo $var . "<br/>"; }
$vars = array("test1", "test2"); for ($i=0; $i<sizeof($vars); $i++) { echo $vars[$i] . "<br/>"; }
Actually, you could do it that way with variable variables. Though the array would be better.
for($i=1; $i<3; $i++) { $res = ${"str_".$i}; echo $res."<br />"; }
Oops, that was a double post.
Good point, LordShryku