JJ2K Posted July 8, 2009 Share Posted July 8, 2009 Hi let's say I have a standard loop like: for($i = 1; $i <10; $i++){ echo $i; } I want to create variables in this loop whose name consist of loop counter but don't know how to do this for example: for($i = 1; $i <10; $i++){ $myField$i = $i; } So I want to create 10 variables called $myField1, $myField2 etc... How can I include the counter in the variable name as using the above would give an error? Link to comment https://forums.phpfreaks.com/topic/165235-solved-loop-help/ Share on other sites More sharing options...
rhodesa Posted July 8, 2009 Share Posted July 8, 2009 first...this is pretty much the reason arrays exist: $myFields = array(); for($i = 1; $i <10; $i++){ $myFields[$i] = $i; } but, to do a dynamic variable: for($i = 1; $i <10; $i++){ ${'myFields'.$i} = $i; } Link to comment https://forums.phpfreaks.com/topic/165235-solved-loop-help/#findComment-871333 Share on other sites More sharing options...
JJ2K Posted July 8, 2009 Author Share Posted July 8, 2009 Thanks very much for not only the answer but directing to me towards what I should have been using (arrays)! Link to comment https://forums.phpfreaks.com/topic/165235-solved-loop-help/#findComment-871346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.