Wo0tHigh Posted February 19, 2008 Share Posted February 19, 2008 Ok so! in a file called "thing1.php" I have the following... <?php $thing1['name']="Dave"; ?> Then in another page I have this... $num='1'; while($num<'2'){ include_once'things/thing'.$num.'.php'; echo'$thing$num[name]<br />';//HERE $num++; } As you can see from the above, where it says HERE, this is where the problem is! I need to display $thing1[name], 1 being retrevied from a variable. Any ideas how? Thank you very much! Dan Link to comment https://forums.phpfreaks.com/topic/91804-help-variables-within-variables/ Share on other sites More sharing options...
marcus Posted February 19, 2008 Share Posted February 19, 2008 $thing = array(); $thing[1]['name'] = "Dave"; $thing[2]['name'] = "Cheryl"; $thing[3]['name'] = "Marcus aka the best name"; for($i=1;$i<=3;$i++){ include_once "things/thing". $i .".php"; echo $thing[$i]['name'] . "<br>\n"; } que? Link to comment https://forums.phpfreaks.com/topic/91804-help-variables-within-variables/#findComment-470182 Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 or a simpler way would be: $num='1'; while($num<'2'){ include_once'things/thing'.$num.'.php'; $varname = "thing".$num; $var = $$varname; echo'$var[name]<br />';//HERE $num++; } Link to comment https://forums.phpfreaks.com/topic/91804-help-variables-within-variables/#findComment-470183 Share on other sites More sharing options...
kenrbnsn Posted February 19, 2008 Share Posted February 19, 2008 The problem is here <?php echo'$var[name]<br />';//HERE ?> Variables contained in strings delimited by single quotes are not expanded. You would want to do something like <?php echo $var[name] . '<br />';//HERE ?> Ken Link to comment https://forums.phpfreaks.com/topic/91804-help-variables-within-variables/#findComment-470184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.