php do while loop

The do ... while loop takes an expression such as a while statement but places it at the end. do ... while loop is useful when you want to execute a block of code at least once regardless of the expression value.

Do While Syntax

<?php
do
{
code to execute;
} while (expression);
?>

Do While Example


<?php
$num = 11;
do
{
echo $num;
$num++;
} while ($num <= 10);
?>

Out Put

11

The above code in the loop displays 11 because the loop always executes at least once. Following the pass, while evaluates to FALSE, causing execution to drop out of the do ... while loop.

Difference between while and do... while

The only difference between a do... while loop and a while loop is that the code within the do block is executed at least once, even if the condition is never true.





Content