Loops
Repeat work with while, for and the array-friendly foreach.
What you will learn
- Write for and while loops
- Loop arrays with foreach
for & while
A loop repeats a block of code so you do not have to write it out many times. A for loop is perfect when you know how many times to repeat. It has three parts inside the brackets: where to start, the condition to keep going, and how to change each time round.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number $i <br>";
}
?>Reading the three parts of for ($i = 1; $i <= 5; $i++): it starts with $i at 1; it keeps looping while $i <= 5; and after each pass $i++ adds 1 to $i. So the body runs with $i equal to 1, 2, 3, 4, then 5 — five times in all — and then $i becomes 6, the condition is false, and the loop ends.
Note: Output (in the browser):
Number 1
Number 2
Number 3
Number 4
Number 5
Each pass prints the current value of $i, and the <br> puts each on its own line. $i++ is what stops the loop from running forever.
A while loop is the other everyday loop. Use it when you do not know in advance how many times to repeat — you just want to keep going as long as a condition stays true. It is simpler-looking than for: you write only the condition in the brackets, and you must remember to change something inside the loop yourself so the condition eventually becomes false.
<?php
$count = 1;
while ($count <= 3) {
echo "Count is $count <br>";
$count++; // step the value yourself
}
?>Step by step: $count starts at 1. PHP checks the condition $count <= 3 — true, so it runs the body, prints Count is 1, then $count++ bumps $count to 2. It checks again (still true) and prints 2, then 3. After printing 3, $count++ makes it 4; now $count <= 3 is false, so the loop stops. The $count++ line is essential — leave it out and $count stays 1 forever, giving an infinite loop.
Note: Output (in the browser):
Count is 1
Count is 2
Count is 3
A for loop bundles the start, condition and change onto one line; a while loop spreads them out — you set the start before the loop and do the change inside it. Both can do the same job; pick whichever reads more clearly.
foreach — the array loop
foreach is the easiest way to loop over an array (a list of values) — you will use it constantly. It hands you one item at a time without you having to count or manage an index.
<?php
$fruits = ["Apple", "Mango", "Banana"];
foreach ($fruits as $fruit) {
echo "$fruit <br>";
}
?>Read foreach ($fruits as $fruit) as "for each item in $fruits, call it $fruit and run this block". On the first pass $fruit holds "Apple", then "Mango", then "Banana" — the loop automatically stops when the list runs out.
Note: Output (in the browser):
Apple
Mango
Banana
No counting, no index — foreach simply visits every element in order. This is why it is the go-to loop for lists of data.
Q. Which loop is best for going through every item in an array?
✍️ Practice
- Print 1–10 with a for loop.
- Loop an array of names with foreach and greet each.
🏠 Homework
- Use a loop to print a multiplication table for a number.