W3docs

do

The "do" keyword is a control structure in PHP that is used to execute a block of code repeatedly until a specified condition is met. In this article, we will

The PHP do Keyword

The do keyword starts a do...while loop in PHP — a control structure that runs a block of code repeatedly as long as a condition stays true. Its defining trait is that the condition is checked at the bottom of the loop, so the body always runs at least once, even when the condition is false from the start.

This page covers the syntax of do...while, how it differs from a regular while loop, the practical situations where it shines (input validation, menus, retry logic), the common mistakes to avoid, and how to control the loop with break and continue.

Syntax

The do keyword pairs with the while keyword. The body comes first, then the condition:

The PHP syntax of do

<?php

do {
  // code to be executed
} while (condition);

Two details to remember:

  • The condition is evaluated after each pass, so the body runs once before it is ever checked.
  • The closing while (condition); must end with a semicolon — unlike a regular while loop, where the semicolon would create a bug.

do...while vs while

The difference is when the condition is tested. A regular while loop checks the condition before the first iteration, so it can run zero times. A do...while loop checks after, so it runs at least once.

<?php

$count = 10;

// while: condition false up front → body never runs
while ($count < 5) {
  echo "while: $count\n";
}

// do...while: body runs once before the check
do {
  echo "do: $count\n"; // prints "do: 10"
} while ($count < 5);

Output:

do: 10

The while loop prints nothing; the do...while loop prints one line. Reach for do...while whenever the body must execute at least once regardless of the starting state.

Examples

Counting and iterating over an array — note that PHP_EOL (or "\n") prints a newline, so each value lands on its own line:

Examples of PHP do

<?php

// Example 1 — count from 1 to 5
$myNumber = 1;
do {
  echo $myNumber . PHP_EOL;
  $myNumber++;
} while ($myNumber <= 5);
// Prints 1, 2, 3, 4, 5 each on its own line

// Example 2 — walk through an array
$myArray = ["apple", "banana", "cherry", "date"];
$index = 0;
do {
  echo $myArray[$index] . PHP_EOL;
  $index++;
} while ($index < count($myArray));
// Prints apple, banana, cherry, date each on its own line

When to use do...while

The "runs at least once" guarantee makes do...while the natural fit for a few recurring patterns.

Retry and validation logic

Try an action once, then keep retrying while it fails:

<?php

$attempts = 0;
do {
  $attempts++;
  // Simulate: the 3rd attempt finally succeeds
  $password = $attempts < 3 ? "wrong" : "secret";
} while ($password !== "secret" && $attempts < 5);

echo "Logged in after $attempts attempts" . PHP_EOL;
// Logged in after 3 attempts

Because the first attempt should always happen, do...while reads more naturally here than a while loop with duplicated setup code.

Display a menu, read input, and repeat until the user chooses to quit — the menu must show at least once:

<?php

$choice = '';
do {
  // show menu, read $choice from input...
  // for this example we just stop immediately
  $choice = 'q';
} while ($choice !== 'q');

echo "Goodbye\n";

Controlling the loop: break and continue

Inside a do...while body you can use break to leave the loop early and continue to skip to the next condition check.

<?php

$n = 0;
do {
  $n++;
  if ($n === 3) {
    continue; // skip printing 3, jump to the while check
  }
  if ($n > 5) {
    break;    // exit the loop entirely
  }
  echo $n . PHP_EOL;
} while (true);
// Prints 1, 2, 4, 5

Note that continue jumps to the condition test, not back to the top of the body — the loop still re-evaluates while (true) and continues.

Common pitfalls

  • Forgetting the trailing semicolon after while (condition) causes a parse error.
  • Infinite loops: if the condition never becomes false (and there is no break), the loop runs forever. Make sure something inside the body changes the values the condition depends on.
  • Off-by-one when indexing arrays: with do...while, the body runs before the bounds check, so an empty array can trigger an "undefined offset" error. Guard with if (!empty($arr)) or use foreach for arrays.

PHP offers several looping constructs — pick the one that matches your intent:

  • while — check the condition before each pass (may run zero times).
  • for — a counter-driven loop with built-in init/condition/increment.
  • foreach — iterate directly over arrays and objects.
  • PHP loops overview — a side-by-side comparison of all four.

Conclusion

The do...while loop is the right tool whenever a block of code must run at least once and then repeat while a condition holds — input validation, retry logic, and menu prompts are classic examples. Remember the trailing semicolon, make sure the condition can eventually become false, and use break/continue to fine-tune the flow.

Practice

Practice
In PHP, what is the purpose of the 'do-while' loop?
In PHP, what is the purpose of the 'do-while' loop?
Was this page helpful?