StraightJ Posted April 26, 2011 Share Posted April 26, 2011 Hello everyone. I am new to PHP, and to programming in general. As of right now, I am reading and learning from "Learning PHP and MySQL" by Michele E. Davis and Jon A. Phillips. In the 'for' loop section of chapter 4, the example they give that actually works, is: <?php for ($num = 1; $num <= 10; $num++) { print "Number is $num<br />\n"; } ?> A question at the end of the chapter requests that I make a 'for' loop counting from 10 to 1. I wrote it as follows: <?php for ($num = 10; $num >= 10; $num--){ print "Number is $num<br />"; } ?> For some reason, my code does not execute, but their example above does. When I checked the appendix answer for creating a 'for' loop counting from 10 to 1, this is the answer the appendix provides: <?php for ($num = 10; $num >= 1; $num−−){ print "$num<br>; } ?> For some reason, the 3rd expression in the code above is not showing up correctly, but it reads, "$num−−" if that helps. I don't agree with the appendix, being that it looks radically different than the example used in the chapter, and i have no idea how to decipher the 3rd expression in the for loop. Can someone tell me what I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/234759-for-loop/ Share on other sites More sharing options...
gristoi Posted April 26, 2011 Share Posted April 26, 2011 Try ($num = 10; $num >= 1; $num--) Quote Link to comment https://forums.phpfreaks.com/topic/234759-for-loop/#findComment-1206444 Share on other sites More sharing options...
Psycho Posted April 26, 2011 Share Posted April 26, 2011 The php manual (www.php.net) has great documentation and would do a better job than I of explaining this than I would. You should always look there first and then post here if you have questions. Here is the page for for loops http://us2.php.net/manual/en/control-structures.for.php But to explain it in my terms, the first expression is run when the loop is first encountered, The second expression determines when the loop should run - as long as the expression results in true the loop will execute. The third expression is executed on each iteration of the loop. In your example for ($num = 10; $num >= 10; $num--){ $num is first set to 10 and the loop will run as long as $num >= 10. Also, $num will be decremented by 1 on each iteration. So, the loop will run one time, then the value of $num will be decreased to 9 and will no longer run due to the condition in the 2nd expression. However, in the book's answer for ($num = 10; $num >= 1; $num--) There is the same initial value of 10, but the 2nd expression states the loop should run as long as $num >= 1, so on the first iteration the loop runs and the value is again reduced to 9, but since 9 >= 1 the loop will run again and again - until the value of num is NOT >= 1. Quote Link to comment https://forums.phpfreaks.com/topic/234759-for-loop/#findComment-1206448 Share on other sites More sharing options...
StraightJ Posted April 26, 2011 Author Share Posted April 26, 2011 Thanks guys. It was really buggin me all morning. I'll test when i get home from work. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234759-for-loop/#findComment-1206455 Share on other sites More sharing options...
gristoi Posted April 26, 2011 Share Posted April 26, 2011 Mjdamato is right, the php.net manually is a life saver. A great reference Quote Link to comment https://forums.phpfreaks.com/topic/234759-for-loop/#findComment-1206458 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.