PHP Loops
In this page:
while (condition) { }
do { } while (condition);
for ($i = 0; $i < $count; $i++) { }
foreach ($array as $item) { }
Why Loops Exist
A loop repeats a block of code automatically instead of you writing that code out multiple times by hand -- printing "Hello" 5 times, processing every row from a database query, or validating every field in a submitted form are all naturally loop-shaped problems.
Example: Why Loops Exist
<?php
// Classic for-loop: `$i = 1; $i <= 5; $i++`
for ($i = 1; $i <= 5; $i++) {
// Print "Hello\n" to the output
echo "Hello\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
The Four PHP Loop Types at a Glance
while runs as long as a condition stays true, checking that condition before every pass. do-while works the same way but checks after each pass, guaranteeing the body runs at least once. for is built for a known, countable number of repetitions. foreach is purpose-built to walk through every element of an array automatically.
Example: The Four PHP Loop Types at a Glance
<?php
// Declare `$i`, set to `0`
$i = 0;
while ($i < 2) { echo "while\n"; $i++; }
// Declare `$j`, set to `0`
$j = 0;
do { echo "do-while\n"; $j++; } while ($j < 2);
for ($k = 0; $k < 2; $k++) { echo "for\n"; }
foreach (["a", "b"] as $v) { echo "foreach: $v\n"; }
?>
Login to try C/C++/Java/PHP code in the editor
Loop Control: break and continue
Inside any loop, break immediately exits the loop entirely, skipping all remaining iterations, while continue skips just the rest of the current iteration and moves on to the next one -- both give you fine control over a loop's flow beyond its basic exit condition.
Example: Loop Control: break and continue
<?php
// Classic for-loop: `$i = 1; $i <= 5; $i++`
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue;
if ($i == 5) break;
// Print `$i . "\n"` to the output
echo $i . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Avoiding Infinite Loops
An infinite loop happens when a loop's condition never becomes false -- most often because the code forgets to update the variable the condition depends on, or updates it in a way that can never actually satisfy the exit condition.
Example: Avoiding Infinite Loops
<?php
$i = 0;
while ($i < 5) {
echo $i . "\n";
$i++; // without this line, the loop would never end
}
?>
Login to try C/C++/Java/PHP code in the editor
Choosing the Right Loop for the Job
Pick while when you don't know in advance how many times you'll repeat but do know the stopping condition; pick do-while when the body must run at least once regardless; pick for when you know exactly how many repetitions you need, often with a counter; pick foreach whenever you are iterating an array's elements.
Example: Choosing the Right Loop for the Job
<?php
// Declare `$items` as an array: `["apple", "banana", "cherry"]`
$items = ["apple", "banana", "cherry"];
// Loop over `$items`, binding each item to `$item`
foreach ($items as $item) {
// Print `$item . "\n"` to the output
echo $item . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Writing a loop condition that never becomes false, creating an infinite loop that hangs the script or exhausts server resources.
- Choosing the wrong loop type for the job -- like a while loop for iterating a known array, when foreach is purpose-built and far less error-prone for that exact case.
- Forgetting to update the loop's counter or condition variable inside the loop body, which is the single most common cause of an infinite while or for loop.
- PHP offers four loop types: while, do-while, for, and foreach -- each suited to a different kind of repetition.
- while checks its condition before each pass; do-while checks it after, guaranteeing at least one run.
- for is built for counting a known number of repetitions; foreach is purpose-built for walking through every item in an array.
All four PHP loop types have been part of the language since its earliest versions and behave identically across every environment.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: