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

PHP while Loop

A while loop is like stirring soup until it is hot: it keeps repeating an action as long as a condition is still true. It checks the condition before every round.
Syntax
php
while (condition) {
    // body
    // update something that affects the condition
}

Simple while Loop

A while loop checks its condition *before* running the body, and keeps re-running that body for as long as the condition stays true. If the condition is false on the very first check, the loop body never executes at all — not even once.

Example: Simple while Loop

php
<?php
// Declare `$i`, set to `1`
$i = 1;
// Keep looping while `$i <= 5` holds
while ($i <= 5) {
    // Print `$i . "\n"` to the output
    echo $i . "\n";
    $i++;
}
?>

Decrementing Loop Counters

Counting down inside a while loop just means subtracting from the loop variable each pass instead of adding to it — the loop mechanics don't change, only the direction the variable moves and, correspondingly, the condition you check against.

Example: Decrementing Loop Counters

php
<?php
// Declare `$i`, set to `5`
$i = 5;
// Keep looping while `$i > 0` holds
while ($i > 0) {
    // Print `$i . "\n"` to the output
    echo $i . "\n";
    $i--;
}
?>

Loops with Boolean Flags

Using a boolean flag as the loop's condition lets code *inside* the loop body decide when to stop, simply by setting that flag to false — useful when the stopping condition depends on something discovered partway through an iteration rather than a value known up front.

Example: Loops with Boolean Flags

php
<?php
// Declare `$keepGoing`, set to `true`
$keepGoing = true;
// Declare `$count`, set to `0`
$count = 0;
// Keep looping while `$keepGoing` holds
while ($keepGoing) {
    $count++;
    // Print `$count . "\n"` to the output
    echo $count . "\n";
    // Check whether `$count == 3`
    if ($count == 3) {
        // Declare `$keepGoing`, set to `false`
        $keepGoing = false;
    }
}
?>

Alternative while Syntax

The colon-based alternative syntax (while (...): ... endwhile;) behaves identically to the brace form but reads more cleanly when a loop is interleaved directly with HTML markup in a template file.

Example: Alternative while Syntax

php
<?php $i = 1; ?>
<?php // Keep looping and printing while $i is 3 or less, incrementing each time ?>
<?php while ($i <= 3): ?>
  <p>Item <?php echo $i; ?></p>
  <?php $i++; ?>
<?php endwhile; ?>

Mathematical Loops

Multiplying or halving a variable each iteration — rather than adding or subtracting a fixed amount — produces exponential growth or decay, which is the pattern behind things like doubling capacity or repeatedly halving a search range.

Example: Mathematical Loops

php
<?php
// Declare `$value`, set to `1`
$value = 1;
// Keep looping while `$value < 100` holds
while ($value < 100) {
    // Print `$value . "\n"` to the output
    echo $value . "\n";
    $value *= 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 to update the loop variable inside the body, creating an infinite loop.
  2. Using <= instead of < with array counts, which runs one iteration too many and reads an undefined index.
  3. Assuming the body runs at least once, when while checks the condition first, so it may never run.

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.