CodesJava

Easy learning with example program codes

PHP Do While Loop


PHP Do While Loop is a loop statement which is used to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails like other loop statements.

The unique feature of Do While loop is that it is used to execute a block of code at least once and then to repeat the actions as long as the specified condition is true.

Syntax:

do 
{
    code to be executed;
} 
while (condition);

Example 1: When the While condition is true

<!DOCTYPE html>
<html>
<body>
 
<?php
$i = 1;
 
do 
{
    echo "$i <br>";
    $i++;
} 
while ($i <= 10);
?>
 
</body>
</html>

Output

1
2
3
4
5
6
7
8
9
10

Example 2: When the While condition is false

<!DOCTYPE html>
<html>
<body>
 
<?php
$i = 21;
 
do 
{
    echo "$i <br>";
    $i++;
} 
while ($i <= 20);
?>
 
</body>
</html>

Output

21
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap