do while Loop in PHP

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax :
do{
      code to be executed;
}while (condition is true);

Example :
<?php
$n=1;
do{
echo $n."<br/>";
$n++;
}while($n<=10);
?>

The above example define a loop that starts with $n=1. It will then increase $n with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as $n is less than, or equal to 10.

Output :
1
2
3
4
5
6
7
8
9
10

0 Comments