W3docs

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:

php— editable, runs on the server

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 ran

Even 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 granted

The 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: 1245

3 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 0

Reach 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]

Practice

Practice
What is the behavior and functionality of the 'do-while' loop in PHP?
What is the behavior and functionality of the 'do-while' loop in PHP?
Was this page helpful?