PHP Do While Loops

What do you do while Doing Do-While Loops?

A regular while loop will skip over the loop body if the expression that’s evaluated is never true. But what if we need the contents of the body to be executed at least once? That’s what the do-while loop does for us.

<?php

$will_execute = false;

while ( $will_execute ) {
    echo "I will execute at least once";
}

?>

Looking at the code above, the while loop will never execute since the expression is always false. PHP will just skip over it. In this example, we want the statement to execute at least once. We want PHP to display “I will execute at least once” regardless of whether the expression evaluates to true or not.

As a recap, the way that the while loop is processed is as follows:

  1. PHP tests the expression inside of the while loop.
  2. If true, execute the code inside of the loop body. Go back to 1 and repeat.
  3. If false, exit the loop and continue code execution after the loop’s closing curly brace.

The do-while loop flips 1 and 2. It starts by executing the content inside of the loop body and then tests the condition. Even if the condition is false, the code inside of the loop body has already been executed once. If the condition is true, it executes the content inside of the loop body again and keeps repeating the process until the loop expression is evaluated to false.

The do-while loop has the following structure:

 

do {
 code to execute
} while ( expression );

 

Let’s change the code in our previous example from a while loop to a do-while loop.

<?php

$will_execute = false;

do {
    echo "I will execute at least once. Part 2";
} while( $will_execute );

?>

This time, the code inside of the loop body executes once before PHP exits the loop. Let’s look at another example. We’ll create a while and a do-while loop that will accomplish the same task: display numbers from 0 to 9.

<?php

$i = 0;

while ( $i < 10 ) {
    echo $i++ . "<br>";
}

$i = 0;

do {
    echo $i++ . "<br>";
} while( $i < 10 );

?>

In the while loop above, PHP checks to make sure that $i is less than 10. During the first iteration, 0 is less than 9. PHP enters the loop body and outputs 0 to the screen. The post-increment operator (++) increments $i from 0 to 1 and PHP tests the condition again. It keeps doing this until 9 is displayed. Once 9 is displayed, $i is incremented to 10, and the expression “$i is less than 10” fails.

In the do-while loop, PHP executes the code inside of the loop body. Value 0 is displayed on the screen and $i is incremented from 0 to 1. The expression is tested: is 0 < 10? Since it is, the contents of the loop body are executed again. The iterations continue until 9 is displayed. The variable $i is incremented from 9 to 10 and the condition fails. The loop ends.

As you can see, in this situation, we can use either the while or the do-while loop since it produces the same result. The only time the results will differ is if $i is initially set to a value that’s higher than 9. Since the while loop tests before executing the code, the code is never reached. The do-while loop executes the code before testing, so it will display the result at least once.

PHP Tutorial Series

Continue your learning with these articles

PHP while loops

This Might take a While

PHP – P29: WHILE LOOPS

The while loop says, “while the expression is true, do something inside of the loop body.” If we passed the value “true” to the conditional expression, the loop would run forever (well, as long as PHP allows it to run before it crashes).

PHP Do While Loops

What do you do while Doing Do-While Loops?

PHP – P30: DO WHILE LOOP

A regular while loop will skip over the loop body if the expression that’s evaluated is never true. But what if we need the contents of the body to be executed at least once? That’s what the do-while loop does for us.

PHP for Loop

LOOP IT UP: UNLEASH THE POWER OF FOR LOOPS!

PHP – P31: FOR LOOP

I use for loops way more than I use while loops. It seems like most of the time I’m reading items from a database: for loops are perfect for that.

Leave a Reply