Skip to content

PHP While Loop

The while loop in PHP is used to execute a block of code repeatedly as long as a given condition evaluates to true.

Syntax

// Generic version of a while loop
while (condition) {
// Code to execute while the condition is true
}

Example

// Concrete example of a while loop
$counter = 1;
while ($counter <= 5) {
echo "$counter ";
$counter++;
}
// Output: 1 2 3 4 5