spence911 Posted November 2, 2013 Share Posted November 2, 2013 I'm a beginner currently going through php loops. I pretty much understand the fundamentals of loops but i can't figure out why this piece of code. <?php $count = 0; while ( true ) { $count++; echo "I ’ ve counted to: $count <br />"; if ( $count == 10 ) break; } ?> results in this output: I ’ ve counted to: 1I ’ ve counted to: 2I ’ ve counted to: 3I ’ ve counted to: 4I ’ ve counted to: 5I ’ ve counted to: 6I ’ ve counted to: 7I ’ ve counted to: 8I ’ ve counted to: 9I ’ ve counted to: 10 If a loop goes back to the starting point after successful completion, why isn't the value of the variable $count equal to 1 each time, thereby outputting "I've counted to: 1 over and over. Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/ Share on other sites More sharing options...
kicken Posted November 2, 2013 Share Posted November 2, 2013 If a loop goes back to the starting point after successful completion, why isn't the value of the variable $count equal to 1 each time, thereby outputting "I've counted to: 1 over and over. There is nothing at the start of the loop that will reset $count variable. It'll maintain whatever value it had at the end of the loop. If you're thinking about the $count = 0;, that line is not part of the loop. The loop body begins with the { just after the while instruction, and ends with the matching } further down. Only the instructions within that area will be repeated as a result of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/#findComment-1456610 Share on other sites More sharing options...
objnoob Posted November 3, 2013 Share Posted November 3, 2013 <?php while ( true ) { // start of while block $count = 0; # reset value to 0 inside the while block $count++; # increment value by 1 echo "I ’ ve counted to: $count <br />"; if ( $count == 10 ) break; # since we reset inside the loop... $count is always 1 thus we never reach 10 and this loops forever } // end of while block ?> Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/#findComment-1456654 Share on other sites More sharing options...
spence911 Posted November 3, 2013 Author Share Posted November 3, 2013 Aaah, finally starting to make sense. I overlooked the fact that only the code within the curly brackets will loop. Thank you both. Especially kicken for pointing that out. But please explain the meaning of $count = 0; while(true){ because that's what threw me off. I was under the impression that it meant: while the value of $count is equal to 0, execute the code after the curly brace. Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/#findComment-1456674 Share on other sites More sharing options...
Solution Barand Posted November 3, 2013 Solution Share Posted November 3, 2013 (edited) while (true) A while loop will execute as long as the condition in the ()s evaluates to true. So while(true) would execute forever without the test to execute the break. You could also achieve the same results with $count = 0; while ( $count < 10 ) { // start of while block $count++; # increment value by 1 echo "I've counted to: $count <br />"; } // end of while block where it gets to "I've counted to ten", goes to the start of the loop again, checks if count is still less than 10 it now is not true (count == 10), so it exits Edited November 3, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/#findComment-1456708 Share on other sites More sharing options...
spence911 Posted November 3, 2013 Author Share Posted November 3, 2013 Thanks a mil Barand, Google turned up nothing solid on explaining the usage and context of while (true). Quote Link to comment https://forums.phpfreaks.com/topic/283539-the-while-loop/#findComment-1456742 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.