PHP Do While Loop
PHP Tutorial > DO WHILE Loop
In PHP the DO / WHILE loop is provided to control conditions. The main idea is that the code will be executed while a condition is true. The basic syntax of DO / WHILE loop is as follows: (Note: The code is executed at least once, and then the condition is tested)
DO {
//enter the code you wish to execute here
} WHILE (conditional statement)
Example
<?php
$counter = 7;
do {
echo 'Hi';
} while ($counter < 6);
?>
The output of the above code is:
Even though $counter is greater than 6, the script will echo "Hi" to the page one time.
In PHP the do/while loop is not commonly used
The main difference between the DO / WHILE and WHILE loop is that DO \ WHILE loop will always execute at least once, in a WHILE loop, it is possible for the code never to execute.
Difference in While Loop and do While loop
In while loop the condition is checked at the starting of the loop and if it is true then the code inside the loop is executed. This process is repeated till the condition becomes false. In case of do while loop the checking of condition is done at the end of the loop. So even if the condition is false, the script inside the loop is executed at least once. This is the basic difference between do while loop and while loop.
Thats It.
![]() |
