Google News
logo
PHP Program to do...while loop
The `do...while` loop is another type of loop statement in PHP that is similar to the `while` loop, but with one important difference.

In a `do...while` loop, the code inside the loop is executed at least once, regardless of whether the condition is true or false.

The basic syntax of a `do...while` loop in PHP is as follows :
do {
    // code to execute at least once
} while (condition);​

In the above syntax, `condition` is an expression that is evaluated after each iteration of the loop. If `condition` is true, then the code inside the loop is executed again. This process repeats until `condition` is false.

Here's an example of a PHP program that uses a `do...while` loop to print the numbers from 1 to 10 :
Program :
<?php
    // This is a PHP program that uses a do...while loop to print the numbers from 1 to 10
    
    $i = 1;
    
    do {
        echo $i . " ";
        $i++;
    } while ($i <= 10);
?>
Output :
1 2 3 4 5 6 7 8 9 10 
In this code, the variable `$i` is initialized to `1`. The `do...while` loop then executes the code inside the loop, which prints the value of `$i`, followed by a space, and increments the value of `$i` by `1`.

After the code inside the loop is executed, the `do...while` loop checks the condition. Since `$i` has been incremented to `2`, the condition is still true, and the code inside the loop is executed again.

This process repeats until `$i` reaches `11`, at which point the condition is no longer true, and the `do...while` loop exits.

This demonstrates how `do...while` loops can be used to execute a block of code at least once, and then repeatedly as long as a certain condition is true.