13. PHP Loops
What you will learn
- How to repeat code with
for,while, anddo...while - How to loop through arrays with
foreach - How to use
breakandcontinue - How to avoid endless loops
Why loops?
A loop runs the same code several times. Write the code once, and let PHP repeat it.
The for loop
PHP code
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Round $i<br>";
}
What the browser shows
Round 1
Round 2
Round 3
Round 4
Round 5
The header has three parts: a start ($i = 1), a condition checked before each round ($i <= 5), and a step after each round ($i++).
The while loop
Use while when you do not know in advance how many rounds are needed.
PHP code
<?php
$money = 100;
$years = 0;
while ($money < 200) {
$money = $money * 1.1;
$years++;
}
echo "It takes $years years to double.";
Output
It takes 8 years to double.
The do…while loop
It checks the condition at the end, so the body always runs at least once.
PHP code
<?php
$n = 10;
do {
echo "n is $n";
} while ($n < 5);
Output
n is 10
The foreach loop
foreach is the loop you will use most. It goes through every item of an array.
PHP code
<?php
$courses = ["HTML", "CSS", "JavaScript", "PHP"];
foreach ($courses as $course) {
echo "I am learning $course<br>";
}
What the browser shows
I am learning HTML
I am learning CSS
I am learning JavaScript
I am learning PHP
To get the key as well, write foreach ($array as $key => $value). You will use this with associative arrays.
break and continue
breakleaves the loop completely.continueskips the rest of this round and starts the next.
PHP code
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 === 0) { continue; }
if ($i > 7) { break; }
echo $i . " ";
}
Output
1 3 5 7
Loops inside HTML
The colon syntax with endforeach keeps templates tidy.
PHP code
<?php $tools = ["Editor", "Browser", "Terminal"]; ?>
<ul>
<?php foreach ($tools as $tool): ?>
<li><?= htmlspecialchars($tool) ?></li>
<?php endforeach; ?>
</ul>
What the browser shows
- Editor
- Browser
- Terminal
Nested loops
PHP code
<?php
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 4; $col++) {
echo "*";
}
echo "<br>";
}
What the browser shows
****
****
****
Endless loops: if the condition never becomes false, the script runs until PHP’s time limit stops it. Make sure something inside the loop changes the condition.
Common mistakes
- Forgetting
$i++, which causes an endless loop. - Off-by-one errors with
<versus<=. - Using
forwith array indexes whenforeachis simpler. - Changing an array while looping over it.
- Printing visitor data in a loop without
htmlspecialchars().
Exercise
Use a loop to add up the numbers from 1 to 10 and print Total: 55.
Show solution
<?php
$total = 0;
for ($i = 1; $i <= 10; $i++) {
$total += $i;
}
echo "Total: $total";
Knowledge check
1. Which loop goes through every item of an array?
foreach
2. What does continue do?
It skips to the next round of the loop.
3. Which loop always runs at least once?
do...while
Summary
forsuits a known count,whilean unknown one, andforeacharrays.breakexits andcontinueskips a round.- Always make sure a loop can finish.