Thursday, July 30, 2026
HomeLanguagesPHP do-while Loop

PHP do-while Loop

The do-while loop is very similar to the while loop, the only difference is that the do-while loop checks the expression (condition) at the end of each iteration. In a do-while loop, the loop is executed at least once when the given expression is “false”. The first iteration of the loop is executed without checking the condition.

Flowchart of the do-while loop:

 

Syntax:

do {
       // Code is executed
} while (if the condition is true);

Example 1: The following code demonstrates the do..while statement.

PHP




<?php
  
    // Declare a number
    $num = 10;
  
    // do-while Loop
    do 
    {
        echo $num . "\n";
        $num += 2;
    } while ($num < 20);
  
?>


Output

10
12
14
16
18

Example 2:

PHP




<?php
  
    // Declare a number
    $num = 0;
  
    // do-while Loop
    do 
   {
        $num += 5;
        echo $num . "\n";
        
    } while ($num < 20);
  
?>


Output

5
10
15
20

Reference: https://www.php.net/manual/en/control-structures.do.while.php

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS