ifreborn Posted June 2, 2006 Share Posted June 2, 2006 What am i doing wrong? how do i make $tom_ incriment?[code]<?php$var = 10; $var1_ = "sam_";for ($i = 1; $i <= "$var"; $i++){$tom_$i = "$var1_$i";print "tom_$i <br>";}####################################### What i would like to see print #######################################/*sam_1sam_2sam_3sam_4sam_5sam_6sam_7sam_8sam_9sam_10*/?>[/code] Link to comment https://forums.phpfreaks.com/topic/11001-for-loop-help/ Share on other sites More sharing options...
kenrbnsn Posted June 2, 2006 Share Posted June 2, 2006 You want to use [a href=\"http://us2.php.net/manual/en/language.variables.variable.php\" target=\"_blank\"]variable variables[/a] here. Try this:[code]<?php$var = 10;$var1 = "sam_";for ($i = 1; $i <= $var; $i++){${"tom_$i"} = $var1 . $i;print ${"tom_$i"} . "<br>";}####################################### What i would like to see print #######################################/*sam_1sam_2sam_3sam_4sam_5sam_6sam_7sam_8sam_9sam_10*/?>[/code]KenBTW, this is much easier if you use an array. Link to comment https://forums.phpfreaks.com/topic/11001-for-loop-help/#findComment-41083 Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 Exactly. During my first months using PHP (actually programming per se was new for me), I tried to do something like this, but believe me, arrays are a much better solution. Link to comment https://forums.phpfreaks.com/topic/11001-for-loop-help/#findComment-41085 Share on other sites More sharing options...
ifreborn Posted June 2, 2006 Author Share Posted June 2, 2006 ${"tom_$i"} is what i what i needed to learn thank you Link to comment https://forums.phpfreaks.com/topic/11001-for-loop-help/#findComment-41086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.