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.
Syntax | Description |
---|---|
do { stmt; } while(expr); |
1) execute stmt first, then check expr 2) execute stmt repeatedly as long as expr is TRUE |
<?php
$var = 0;
do
{
echo "$var\r\n"; // execute statement at least once
$var++;
sleep(1);
}while($var < 3);
?>
[result]
0
1
2