PHP while Loop
In this page:
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
// 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++;
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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--;
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
}
?>
Login to try C/C++/Java/PHP code in the editor
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 $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; ?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting to update the loop variable inside the body, creating an infinite loop.
- Using
<=instead of<with array counts, which runs one iteration too many and reads an undefined index. - Assuming the body runs at least once, when
whilechecks the condition first, so it may never run.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: