Learning PHP 101 – While & Do-While Loops

Loops are structures that can execute a set of statements multiple times. They save us time and make code management simpler. Each type of loop serves to fit different scenarios. It is important that you chose the right loop for the job.

Remember:

  • The for loop is used to loop through a block of code a specified number of times.
  • The foreach loop is used to loop through array items and execute a block of code for each item.

The while loop is used to execute a block of code for as long as the specified condition is true.

Syntax

<?php
    while (condition is true) {
        code to be executed;
    } 
?>

The while loop has many uses and here is a simple example of how it differs from the for loop.

<?php
    $i = 0; // loop variable
    while($i < 10) { // condition
        // code block
        echo "<p>$i is less than 10!</p>";
        $i++; // counter
    }
?>

Take note that the loop variable is outside of the condition but our counter is inside the loop.

Here is a diagram of the above loop.

example-while-loop-diagram

Figure 1: Example while loop.

Since a while loop will continue to run until the condition is false it is important that you ensure your code has an endpoint.

The next example has no way to exit and is called an infinite loop. An infinite loop will not stop for as long as the system its running on can handle it. There is no good practical reason to use infinite loops and should be avoided!

<?php
    $i = 1;
    while($i > 0) {
        echo "<p>$i is greater than 0!</p>";
        $i++;
    }
?>

If however you find yourself in a situation where you have infinite loop, you can stop them by declaring the loop variable as false.

<?php
    $i = 1;
    while($i > 0) {
        echo "<p>$i is greater than 0!</p>";
        $i = false; // exit the loop!
    }
?>
Note: An Infinite loop will cause a browser to continuously load and eventually time out due to the servers timeout settings. PHP will only output the results once the function is complete and in the case of infinite loops, that will never happen.

Alternate Syntax

Just like with control flows, loops can also have alternate syntax’s. Again you can chose which one your preference, but you should also be aware of different ways of writing the same thing.

<?php
    while(condition):
       // looped statements go here
    endwhile;
?>

Note that with this syntax the curly braces {} have been removed and instead you will see a colon (:) after the condition and a semicolon (;) after the keyword endwhile.

Here is our example converted to the alternative syntax.

<?php
    $i = 1;
    while($i > 0):
        echo "<p>$i is greater than 0!</p>";
        $i = false; // exit the loop!
    endwhile;
?>

Looping with Do-While

A while loop check the condition before each iteration. A do/while loop checks the condition after each iteration.

A consequence of do/while loops is that the code is executed at least once whereas a while loop has to meet the initial condition (i.e. be true) before it can execute the code block.

<?php
    $i = 10;
    do {
        echo "<p>This Loop ran even though the condition is false</p>";
        $i++;
    } while($i < 10);
?>

As you can see, if $i was equal to 10 the code block would still execute and in the case above, would result in an incorrect output because 10 is not less than 10;

Summarize

Different loops serve different purposes so remember that:

  • while – loops through a block of code and will continue to do so for as long as the specified condition is true.
  • do/while – loops through a block of code at least once, and then repeats the loop for as long as the specified condition is true.

Next: we will look at some built in functions!

In Category: PHP tutorials

Matthew Horne

I am a web developer from the United kingdom who taught himself PHP and JavaScript and continues to build on those skills as well as learn new skills.

Show 0 Comments
No comments yet. Be the first.

Leave a Comment