twilitegxa Posted November 7, 2010 Share Posted November 7, 2010 I have the following code that cycles though and prints out the day of the week and stops printing after the seventh day is reached, but it keeps looping. How do I stop it from looping after 7? I thought I had it right, but it's not. Can anyone help? <?php $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); echo current($weekdays) . "<br />"; while (count($weekdays) < { echo next($weekdays) . "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/218047-stopping-loop-help/ Share on other sites More sharing options...
DavidAM Posted November 7, 2010 Share Posted November 7, 2010 count($weekdays) is not going to change. It is the number of entries in the array. try using foreach instead $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); foreach($weekdays as $wkDay) { echo $wkDay . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/218047-stopping-loop-help/#findComment-1131536 Share on other sites More sharing options...
Vitamin Posted November 7, 2010 Share Posted November 7, 2010 $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); echo current($weekdays) . "<br />"; $i = 0; while ($i < count($weekdays)) { echo next($weekdays) . "<br />"; $i++; } Link to comment https://forums.phpfreaks.com/topic/218047-stopping-loop-help/#findComment-1131537 Share on other sites More sharing options...
DavidAM Posted November 8, 2010 Share Posted November 8, 2010 OR $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); for($i = 0; $i < count($weekdays); $i++) { echo $weekdays[$i] . "<br />"; } * Not really recommended since the count() will be executed every time the loop starts. Or, if you like writing code that is on one line and difficult to read or maintain: $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); for($i = 0; $i < count($weekdays); echo $weekdays[$i++] . "<br />") ; * Also NOT recommended by the way, if you are going to use next(), you should probably use reset() instead of current() for the first one. If you try to execute the loop a second time without a reset, the pointer will be at the end of the array and you will get an error: $weekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); echo reset($weekdays) . "<br />"; $i = 0; while ($i < count($weekdays)) { echo next($weekdays) . "<br />"; $i++; } Link to comment https://forums.phpfreaks.com/topic/218047-stopping-loop-help/#findComment-1131559 Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 Of course, you're still using count within the loop. Not that it matters much in such a small array. Personally, I'd use the foreach and increment a variable if I needed to keep track of numbers. 1st reply, simple and effective. Link to comment https://forums.phpfreaks.com/topic/218047-stopping-loop-help/#findComment-1131561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.