← Back to PHP Course | Chapter 4: Control Flow | Lesson 11 of 14

PHP do-while Loop

A do-while loop is like tasting the soup first and then deciding whether to keep cooking. It always runs the code at least once, and only then checks whether to go around again.
Syntax
php
do {
    // body runs at least once
} while (condition);

Basic do-while Loop

A do-while loop is a post-test loop: it runs the loop body first, and only checks the condition *after* that first run has already completed — the opposite order from a regular while loop, which tests before ever entering the body.

Example: Basic do-while Loop

php
<?php
// Declare `$i`, set to `1`
$i = 1;
// Run this block once, then repeat while the trailing `while` condition holds
do {
    // Print `$i . "\n"` to the output
    echo $i . "\n";
    $i++;
} while ($i <= 3);
?>

Guaranteeing One Run

Because the condition check happens after the body runs, a do-while loop is guaranteed to execute at least once, even if the condition would have evaluated to false immediately — this is the one meaningful behavioral difference from a plain while loop.

Example: Guaranteeing One Run

php
<?php
// Declare `$i`, set to `10`
$i = 10;
// Run this block once, then repeat while the trailing `while` condition holds
do {
    // Print "This runs at least once, i=$i\n" to the output
    echo "This runs at least once, i=$i\n";
} while ($i < 5);
?>

Updating Variables inside Loop

The variable your condition checks has to change somewhere inside the loop body, or the condition will keep re-evaluating to the exact same result forever — an infinite loop that never terminates, since nothing inside the loop is moving it toward the stopping point.

Example: Updating Variables inside Loop

php
<?php
$i = 0;
do {
    echo $i . "\n";
    $i++; // must update, or this loop never ends
} while ($i < 3);
?>

Simulation of Menu Runs

A menu that should display its options at least once — even before the user has made any choice yet — is a natural fit for do-while: you want the 'show options' step to run first regardless, then keep repeating only if the user asks for another round.

Example: Simulation of Menu Runs

php
<?php
// Declare `$choice`, set to "exit"
$choice = "exit";
// Run this block once, then repeat while the trailing `while` condition holds
do {
    // Print "1. Start\n2. Settings\n3. Exit\n" to the output
    echo "1. Start\n2. Settings\n3. Exit\n";
} while ($choice !== "exit");
?>

Nested do-while Loops

Nesting one do-while loop inside another lets an inner loop run its full guaranteed-at-least-once cycle for every single pass of the outer loop, which is useful for building grid-like or multi-dimensional repeated structures.

Example: Nested do-while Loops

php
<?php
// Declare `$i`, set to `1`
$i = 1;
// Run this block once, then repeat while the trailing `while` condition holds
do {
    // Declare `$j`, set to `1`
    $j = 1;
    // Run this block once, then repeat while the trailing `while` condition holds
    do {
        // Print "$i,$j  " to the output
        echo "$i,$j  ";
        $j++;
    } while ($j <= 2);
    // Print "\n" to the output
    echo "\n";
    $i++;
} while ($i <= 2);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon after while (condition); at the end of do-while, which is a parse error.
  2. Forgetting to change the condition variable inside the body, causing an infinite loop.
  3. Using do-while when the body should not run if the condition is false from the start, since it always runs once.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.