PHP Loops | do..while

Hi everyone,

do..while loop is executed at least once and if the condition in the end is true the code-block inside loop is executed. Here is the syntax:

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

Here goes an example:

output will be :

This is the first time! Value of $a = 3 
Value of $a = 2
Value of $a = 1

<?php
$a = 3;
do {
    if ($a == 3)
        echo "This is the first time! Value of \$a = 3 </br>";
    else
        echo "Value of \$a = " . $a . "</br>";
    $a--;
} while ($a > 0);
?>

Note: We used \ before writing $a to show $a as a string not its value. If we had not put it before $a we would see its value even if it is inside quotation marks.

Now you have learned do..while loop in PHP. Stay cool and stay connected to learn more!.. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *