PHP : do while Loop

In PHP, a do while loop is similar to a while loop, but it checks the condition after executing the statements inside the loop. This means that the loop will always execute at least once, regardless of whether the condition is initially true or not. 

Here's the basic syntax:


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

Here's a breakdown of how it works:

1. The code block within the do and while braces is executed first.
2. Then, the condition is evaluated.
3. If the condition is true, the loop will execute again. If the condition is false, the loop will terminate and the program will continue execution after the loop.

Here's an example to demonstrate how a do while loop works:


<?php
$count = 1;

do {
    echo "The count is: $count <br>";
    $count++;
} while ($count <= 5);
?>

 

In this example, the loop will output:


The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5

 

Even though the condition $count <= 5 evaluates to false after the fifth iteration, the loop still executes once because the condition is checked after the code block is executed.