PHP Do While

It may be beneficial for you to be somewhat familiar with how the “while” loop is used before you can fully understand how the “do while” loop works.

For more PHP loop examples, see:

PHP For Loop
PHP Foreach Loop
PHP While Loop

As a brief recap, while loops are executed if the conditional statement is true. If it is false, then the loop is not executed.

A do while loop is a spin-off of the while loop in that the do while loop will execute its loop at least one time because its conditional statement is checked to be true or false after the loop has been executed.

An example of a while loop can look like:

$online = 0;
while($online >= 1){
echo “You are connected to the Internet.”;
}

In the while loop example above, if the conditional statement ($online) is greater than or equal to 1, the loop is executed. Because the conditional statement ($online) equals 0, which is less than 1, the loop is not executed, therefore nothing is displayed.

Alternatively, you can use a do while loop, which can look like:

$online = 0;
do {
echo “You are connected to the Internet.”;
} while ($cookies >= 1);

This bit of code would display:

You are connected to the Internet.

The reason the conditional statement is echoed is because the “do” condition is told to echo “You are connected to the Internet.” before it calculates the “while” condition.

For more PHP loop examples, see:

PHP For Loop
PHP Foreach Loop
PHP While Loop