Loose_Goose Posted July 30, 2013 Share Posted July 30, 2013 (edited) Please be patient with me as I am only a beginner. I am trying to learn this on my own using online tutorials. I have written the following code; <?php $counter=30; while ($counter>-1) { echo $counter; $counter=$counter+-1; if ($counter==0){ while ($counter<30) { echo $counter++; $counter=$counter++; } } } ?> It runs continuously. I want it to count down to zero, up to 30 again, and then stop. I also want to know how I can get each number to appear on a separate line instead of all the numbers appearing on the same line. Edited July 30, 2013 by ignace code tags plus indentation Quote Link to comment https://forums.phpfreaks.com/topic/280651-more-questions-from-a-beginner/ Share on other sites More sharing options...
ignace Posted July 30, 2013 Share Posted July 30, 2013 (edited) // count down from 30 to 0 for ($i = 30; $i >= 0; $i = $i - 1 /* long version to better understand what happens <=> $i -= 1 <=> $i-- */) { echo $i, '<br>', "\n"; } // count up from 0 to 30 for ($i = 0; $i <= 30; $i = $i + 1 /* long version to better understand what happens <=> $i += 1 <=> $i++ */) { echo $i, '<br>', "\n"; } Edited July 30, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/280651-more-questions-from-a-beginner/#findComment-1442721 Share on other sites More sharing options...
Loose_Goose Posted July 30, 2013 Author Share Posted July 30, 2013 (edited) Thanks Edited July 30, 2013 by Loose_Goose Quote Link to comment https://forums.phpfreaks.com/topic/280651-more-questions-from-a-beginner/#findComment-1442723 Share on other sites More sharing options...
cyberRobot Posted July 31, 2013 Share Posted July 31, 2013 (edited) If you don't need a loop, you could also do something like this: <?php $numbers = range(1, 30); print implode('<br>', $numbers); print '<br>'; $numbers = array_reverse($numbers); print implode('<br>', $numbers); ?> An example of using range() in a foreach loop can be found here: http://php.net/manual/en/function.range.php Edited July 31, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/280651-more-questions-from-a-beginner/#findComment-1442786 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.