Thursday, June 4, 2009

PHP - Do while

A do-while loop checks the test condition (the condition that is defined inside the while) at the end of the loop. Regular while loops check the test condition at the beginning of the loop.

PHP While loop

<?php
$i = 0;
while($i > 0){
echo $i;
}
?>

As you can see, this while loop's conditional statement failed ($i is not greater than 0), which means the code within the while loop was not executed

PHP Do While loop


<?php
$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.