PHP do while loop
Description
The PHP do...while construct is similar to a while loop. The difference is that the do...while loop is executed at least once.
Syntax
The do while loop has the following syntax.
do{
loop body
}while(condition is true);
Example
Consider the following piece of code:
<?php//from www . j a v a 2 s .c om
$i = 1;
do {
print "Number $i\n";
} while ($i < 10);
?>
The code above generates the following result.
Example rewrite
In comparison, that same code could be written using a while loop:
<?php/* w w w . j a v a2s .c om*/
$i = 1;
while ($i < 10) {
print "Number $i\n";
}
?>
The difference is that the while loop would output nothing, because it checks the value of $i before entering the loop. Therefore, do...while loops are always executed a minimum of once.
The code above generates the following result.