horsebox Posted April 17, 2010 Share Posted April 17, 2010 I don't really know how to use loops I've been using the for loop for everything but I keep running into the same problem. When I need to do something once inside the loop like for example echo $variable it does it 50 times because its inside the loop. How do you do something only once when you're inside a loop thats set to repeat multiple times? For example say I have the loop for ($ibr=0; $ibr<=10; $ibr++) { $var[$ibr] = $ibr + 4; echo $variable; } or whatever and need to repeat the $ibr + 4 part 10 times but only need to echo the $variable once what would I do? The way it is there it will echo the $variable 10 times. Link to comment https://forums.phpfreaks.com/topic/198857-how-to-use-loops/ Share on other sites More sharing options...
Daniel0 Posted April 17, 2010 Share Posted April 17, 2010 Move it outside the loop? Link to comment https://forums.phpfreaks.com/topic/198857-how-to-use-loops/#findComment-1043810 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 Either like Daniel stated, or.. I guess you could do the pointless method of this.. for ($ibr=0; $ibr<=10; $ibr++) { $var[$ibr] = $ibr + 4; if($ibr == '1') { echo $variable; } } Link to comment https://forums.phpfreaks.com/topic/198857-how-to-use-loops/#findComment-1043815 Share on other sites More sharing options...
leehanken Posted April 17, 2010 Share Posted April 17, 2010 Also check out 'while', and 'do-while' http://www.php.net/manual/en/language.control-structures.php Link to comment https://forums.phpfreaks.com/topic/198857-how-to-use-loops/#findComment-1043862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.