Here you will learn about PHP Do while loop. But, before going further, you must first learn PHP While Loop, because, the do-while loop is very similar to while-loop.


PHP Do While Loop


हिन्दी | PHP Do While Loop in Hindi


Difference between the Do-While loop and While-Loop

There is only one difference between the do-while loop and the while-loop. The while-loop first checks the condition and then executes the block of code, but the do-while loop first executes the block of code then checks the condition.

Both Loops are executing the block of code according to the condition, but the PHP Do-While loop executes the block of code at least once because it will check the condition after executes the block of code.


Syntax of Do-While Loop

<?php
do{
  // Block of Code
}while(condition);
?>

How does work the PHP While loop?

PHP While Loop

Example of PHP While Loop

The following example displays numbers from 1 to 5.

<?php
$i = 1;
do{
    echo $i.'<br>';
    $i++;
}while($i <= 5);
?>
Browser Output
1
2
3
4
5

Questions and Answers

What is the difference between the Do-While loop and While-Loop?

The PHP While-Loop and the Do-While Loop executing the block of code or tasks according to the condition. But the PHP Do-While loop executes the block of code at least once because it will check the condition after executes the block of code.

So the main difference between the Do-While loop and While-Loop is while-loop checks the condition before executes the block of code and the do-while loop checks the condition after executes the block of code.

Leave a Reply

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