Jump to content

The while loop


spence911
Go to solution Solved by Barand,

Recommended Posts

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: 1
I ’ ve counted to: 2
I ’ ve counted to: 3
I ’ ve counted to: 4
I ’ ve counted to: 5
I ’ ve counted to: 6
I ’ ve counted to: 7
I ’ ve counted to: 8
I ’ ve counted to: 9
I ’ 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites


<?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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution
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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.