In PHP programming, when you want to execute a block of code at least once before checking any condition, the PHP do while loop is the perfect choice. This loop is beginner-friendly and very useful in real projects.
| PHP do while loop explained with syntax and simple example |
If you have already learned PHP while loop or PHP for loop, then understanding the PHP do while loop will make your loop concepts even stronger.
What is PHP do while Loop?
PHP do while loop is a looping statement that executes the code block first and then checks the condition. This means the loop will run at least one time, even if the condition is false.
This behavior makes the do while loop different from the while loop, where the condition is checked before execution.
PHP do while Loop Syntax
The syntax of PHP do while loop is simple and easy to understand:
do {
// code to be executed
} while (condition);
First, the code inside the do block executes. After that, the condition inside while() is checked. If the condition is true, the loop runs again.
Simple PHP do while Loop Example
<?php
$i = 1;
do {
echo $i . "<br>";
$i++;
} while ($i <= 5);
?>
In this example, the value of $i starts from 1. The loop prints numbers from 1 to 5. Even if the condition becomes false, the loop runs once before stopping.
Funny Real-Life Example of PHP do while Loop (Alarm Snooze 😂)
Let’s understand PHP do while loop with a funny real-life example that everyone can relate to.
Situation: You set an alarm in the morning. The alarm will ring at least once, and it will keep ringing until you finally wake up.
This is exactly how a do while loop works.
<?php
$wakeUp = false;
$alarmCount = 1;
do {
echo "Alarm Ringing... Snooze pressed! <br>";
$alarmCount++;
if ($alarmCount > 3) {
$wakeUp = true;
}
} while ($wakeUp == false);
echo "Finally woke up and alarm stopped!";
?>
Explanation: The alarm rings at least once because the code inside do runs first. The condition is checked later. This loop continues until the user finally wakes up. That is why PHP do while loop guarantees at least one execution.
Difference Between while Loop and do while Loop in PHP
| while loop | do while loop |
|---|---|
| Condition is checked before execution | Condition is checked after execution |
| May not execute even once | Executes at least once |
| Used when condition must be true first | Used when code must run once |
When to Use PHP do while Loop?
You should use PHP do while loop when:
- User input must be processed at least once
- Menu-driven programs are required
- Validation must run before checking condition
This loop is commonly used with PHP conditional statements in real-world applications.
Common Mistakes in PHP do while Loop
- Missing semicolon: The semicolon after
while(condition);is mandatory. - Infinite loop: If the condition never becomes false, the loop will never stop.
PHP do while Loop FAQs
Does PHP do while loop run at least once?
Yes, PHP do while loop always executes at least one time.
What is the main difference between while and do while loop?
The main difference is condition checking. While loop checks condition first, do while loop checks it later.
Conclusion
PHP do while loop is a powerful looping statement when you want your code to run at least once. With clear syntax and real-life examples like the alarm snooze, this loop becomes very easy to understand.
After learning this loop, you should also explore:PHP while loop, PHP for loop and PHP foreach loop to master PHP looping concepts completely.