PHP do while loop
In this chapter you will learn:
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//jav a2 s . c om
$i = 1;
do {
print "Number $i\n";
} while ($i < 10);
?>
In comparison, that same code could be written using a while loop:
<?php/*from j a v a 2s . 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.
Next chapter...
What you will learn in the next chapter:
- What is for loop
- for loop Syntax
- Example - for loop with
- Example - Infinite Loops
- Example - Loops Within Loops