PHP do while Loop
The PHP do...while Loop
The PHP do...while loop - Loops
through a block of code at least once, and then repeats the loop as long as the specified
condition is true.
Syntax
do {
// code to be executed
} while (condition);
Note: In a
do...while loop the condition is tested after executing
the code within the loop. This means that the loop will execute
at least once, even if the condition is false.
Example
Set $i = 1, then print $i
as long as $i is less than 6:
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
Try it Yourself »
Here, we set the variable $i to 8 instead of 1,
and execute the same do...while loop again:
Example
Set $i = 8, then print $i
as long as $i is less than 6:
$i = 8;
do {
echo $i;
$i++;
} while ($i < 6);
Try it Yourself »
The PHP break Statement
With the break statement we can stop the loop even if the condition is still true:
Example
Stop the loop when $i is 3:
$i = 1;
do {
if ($i == 3) break;
echo $i;
$i++;
} while ($i < 6);
Try it Yourself »
The PHP continue Statement
With the continue statement we can
skip the current iteration, and continue with the next:
Example
Skip, and move to the next iteration if $i is 3:
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
Try it Yourself »