Menu

PHP while Loop and do while: Syntax and Examples

while (condition) { ... } repeats a block as long as the condition is true, checking it before every pass. do { ... } while (condition); checks after, so it always runs at least once. Covers while (true) with break, reading a file line by line, and avoiding infinite loops.

This page includes runnable editors - edit, run, and see output instantly.

while (condition) { ... } runs the block again and again as long as the condition is true. The condition is checked before every pass, so if it is false from the start, the body never runs. Something inside the loop has to change the condition, or it never ends.

This is the kind of question while is made for: you do not know the number of passes in advance, only when to stop. Change the interest rate and run it again.

do while: run at least once

do { ... } while (condition); checks the condition after the body, so the body always runs once. Note the semicolon at the end.

A typical use is "try, then check": roll a die until you get a six, generate a code until it is unused. The first attempt has to happen before there is anything to check. This one gives a different result every run:

while (true) with break

When the exit condition is easier to check in the middle of the body, write while (true) and leave with break. continue skips to the next check, as in any loop.

array_shift() returns null once the array is empty, which is the other way out. The same loop could be written as while ($queue), because an empty array counts as false.

Read a file line by line

fgets() returns one line at a time and false at the end of the file, so it drives a while loop directly. The block writes the file first, then reads it back.

The parentheses around $line = fgets($handle) matter: the assignment happens first, then the result is compared with false. Using !== false instead of a plain truthy check keeps a last line that holds just 0 (with no newline after it) from ending the loop early. The fopen() page covers the file functions.

while with endwhile in templates

Like if and for, while has a colon form for HTML: while (...): and endwhile;. This one prints a countdown list in the Page tab.

Common mistake: the loop that never ends

The counter has to move toward the exit. Forget $i++, or change the wrong variable, and the condition stays true forever. On a web server PHP stops the script after max_execution_time (30 seconds by default) with a fatal error. On the command line it never stops.

// Never ends: $i is never changed
$i = 0;
while ($i < 3) {
    echo $i;
}

A safety limit is a cheap guard for loops that depend on outside data, such as retrying a call or paging through results:

Run it a few times: sometimes it succeeds early, sometimes it gives up, but it always ends. When you know the number of passes up front, a for loop states the start, end and step in one line and is harder to get wrong.

Frequently Asked Questions

What is the difference between while and do while in PHP?

while checks the condition before each pass, so the body may never run. do { ... } while ($cond); checks after each pass, so the body always runs at least once. Note the semicolon after the do while condition.

How do I stop an infinite while loop in PHP?

Inside the loop, end it with break when your exit condition is met: while (true) { ...; if ($done) { break; } }. If the loop has no break and its condition never becomes false, the script runs until PHP's max_execution_time stops it (web requests) or forever (the command line).

When should I use while instead of for in PHP?

Use while when you do not know how many passes you need: reading until a file ends, retrying until something succeeds, processing a queue until it is empty. Use for when you count, and foreach to visit every element of an array.

Does PHP have a do until loop?

No. Negate the stop condition in a do while: do { $n++; } while (!($n >= 3)); runs until $n >= 3 is true. until is not a PHP keyword.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED