Understanding PHP Looping with do-while Statement
Loops in PHP are an essential concept to master, as they allow you to perform a task multiple times without repeating yourself. One of the looping statements in
Loops let you run the same block of code many times without writing it out by hand. PHP has four loop constructs — for, while, do-while, and foreach — and the do-while loop is the one you reach for when the body must run at least once, no matter what the condition says.
This is what makes do-while different from a regular while loop: a while loop checks its condition before the first iteration, so the body may never run. A do-while loop checks the condition after each iteration, so the body always runs once and then repeats as long as the condition stays true.
This chapter covers the syntax, how do-while differs from while, a classic real-world use case (input validation), how break and continue behave inside it, and the common off-by-one gotcha.
Syntax
do {
// code to be executed each iteration
} while (condition);The body sits inside the do { ... } block, and the condition is tested after the body runs. Note the semicolon after while (condition) — it is required here (unlike a regular while loop) and forgetting it is a frequent syntax error.
The flow is: run the body once → evaluate the condition → if it is truthy, run the body again → repeat → stop the first time the condition is falsy.
A first example
This prints the numbers 1 to 10:
The body runs, prints $i, increments it, then the condition $i <= 10 is checked. Once $i reaches 11 the condition is false and the loop ends, so the digits 1 through 10 are printed.
do-while vs. while: the key difference
The body of a do-while loop always executes at least once, even when the condition is false from the start. A while loop, by contrast, may execute zero times. Compare:
<?php
// do-while: body runs once, THEN the condition is checked
$x = 100;
do {
echo "do-while ran\n";
} while ($x < 10);
// while: condition checked first, so the body never runs
$y = 100;
while ($y < 10) {
echo "while ran\n";
}
// Output:
// do-while ranEven though 100 < 10 is false in both cases, the do-while body still printed once. Use do-while whenever "run this, then decide whether to repeat" matches your logic.
Real-world use: validating input
The "run at least once" behaviour is perfect for prompting a user (or retrying an operation) until the result is acceptable — you always need at least one attempt before you have something to validate:
<?php
$attempts = 0;
$pin = "";
do {
$attempts++;
// In real code this would read from input; we simulate the attempts:
$pin = ["123", "0000", "4821"][$attempts - 1];
echo "Attempt $attempts: tried '$pin'\n";
} while ($pin !== "4821" && $attempts < 3);
echo $pin === "4821" ? "Access granted\n" : "Locked out\n";
// Output:
// Attempt 1: tried '123'
// Attempt 2: tried '0000'
// Attempt 3: tried '4821'
// Access grantedThe loop keeps going until the correct PIN is entered or the attempt limit is reached — and it never needs a separate "first attempt" before the loop.
break and continue
break exits the loop immediately, and continue skips to the condition check (the next iteration):
<?php
$i = 0;
do {
$i++;
if ($i === 3) {
continue; // skip printing 3
}
if ($i > 5) {
break; // stop entirely
}
echo $i;
} while (true);
// Output: 12453 is skipped by continue, and break stops the loop after 5, so 1, 2, 4, 5 are printed.
Common gotcha: the off-by-one trap
Because do-while runs before checking, an "empty" condition still produces one iteration. If you process a collection that might be empty, prefer foreach or while — a do-while would process one nonexistent element first:
<?php
$items = []; // empty!
$index = 0;
do {
echo "Processing item $index\n"; // runs once even though there are no items
$index++;
} while ($index < count($items));
// Output: Processing item 0Reach for do-while only when "at least once" is genuinely what you want.
Flowchart
graph LR
A[Start] --> B[Execute code block]
B --> C[Check condition]
C --> D{Is condition true?}
D --Yes--> B
D --No--> E[End]Related chapters
- PHP while Loop — checks the condition first; body may run zero times.
- PHP for Loop — best when you know the iteration count in advance.
- PHP foreach Loop — the idiomatic way to iterate arrays.
- PHP break / continue — controlling loop flow.