PHP - Do While loop

PHP - Do While loop

PHP - Do While loop


Do-while loops are very similar to while loops, except the expression, is checked at the end of each iteration.

Diagram of loop do-while:

do-while



The syntax of do-while:

<?php
    do
    {
        // code to be executed
    }
    while(expression);
?>

As the expression is checked at the end of the loop, the first iteration of a do-while loop is guaranteed to run!

<?php
     
    $x = 5;
    do
    {
        $x ++;
        echo ($x);       
    }
    while($x > 5);
?>

The code will be executed, in spite of expression is false, and the output will be :

Reactions

Post a Comment

0 Comments

close