Friday, August 25, 2017

PHP Do While Loop


do-while loop is very similar to while loop. In a do-while loop, the test condition evaluation is at the end of the loop.  This means that the code inside of the loop will iterate once through before the condition is ever evaluated.

Syntax :
do{  
//code to be executed here  
}while(condition);

In above syntax loop would run one time exactly, since after the first iteration, when condition is checked, it evaluates to FALSE then the loop execution ends Otherwise it will iterate the loop.

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

Output:
--------------------------------
0
1
2
3
4
5
6
7
8
9

10

Example : 2
<?php
$i = 0;
do {
    echo $i;
} while ($i > 0);
?>

Output:
-------------------
0



No comments:

Post a Comment