PHP Do While Loop

••• PHP Do While Loop

The PHP Do While Loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

PHP DO While Loop

do-while loops are very similar to while loops, except the truth expression, is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).

Syntext of PHP Do While Loop:

do {
 // code block to be executed
} while (expression);

The code block inside PHP Do While Loop statement executes first and then the expression is checked at the end of each iteration. If the expression evaluates to true, the code block executes repeatedly until the expression evaluates to false.

There is just one example of PHP Do While Loop:

$i = 0;
do {
    echo $i;
} while ($i > 0);

The above loop would run one time exactly since, after the first iteration, when truth expression is checked, it evaluates to FALSE ($i is not bigger than 0) and the loop execution ends.