Loops in PHP (for, while, do-while, foreach)
In this lesson: Learn all PHP loops with syntax, examples and output — print tables, loop arrays with foreach, break and continue. Hindi & English.
What loops are for
A loop repeats a block of code many times, so you do not have to write it again and again. Want to print 1 to 100, or go through every item in a list? A loop does it in a few lines. PHP has four loops: for, while, do-while and foreach.
The for loop
Use for when you know how many times to repeat. It has three parts: start, condition, and update.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
?>
It reads as: start at $i = 1, keep going while $i <= 5, and add 1 each round ($i++). When $i becomes 6 the condition is false and the loop stops.
while and do-while
while checks the condition first; do-while runs once then checks.
<?php
$i = 1;
while ($i <= 3) {
echo $i . " ";
$i++;
}
// do-while always runs at least once:
$n = 10;
do {
echo "run ";
} while ($n < 5);
?>
Even though $n < 5 is false, the do-while printed "run" once because it checks the condition after running the body.
foreach (for arrays)
foreach is the easiest way to go through an array — no counter needed.
<?php
$fruits = ["Apple", "Mango", "Banana"];
foreach ($fruits as $fruit) {
echo $fruit . " ";
}
?>
For key-value arrays you can grab both: foreach ($ages as $name => $age) gives you the name and the age together each round.
break and continue
break leaves the loop early; continue skips just the current round.
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue; // skip 3
if ($i == 5) break; // stop before 5
echo $i . " ";
}
?>
3 is skipped by continue, and the loop stops at 5 because of break — so only 1, 2 and 4 print.
Common mistakes
- Forgetting to update the counter. Missing
$i++gives an infinite loop. - Off-by-one errors.
<=vs<changes how many times the loop runs — check the boundary. - Using for when foreach is cleaner. For arrays, foreach avoids index mistakes.
- Changing the array while looping over it. This can cause skipped or repeated items.
Summary
- Loops repeat code so you write less and do more.
for— when you know the count;while— repeat until a condition changes.do-whilealways runs at least once;foreachis best for arrays.breakexits the loop;continueskips one round.- Always ensure the loop can reach its end, or it runs forever.
Next, store the values you loop over with arrays in PHP, or make choices inside loops with if-else in PHP.
Frequently Asked Questions
What is the difference between for and while loops?
for loop when you know how many times to repeat (like 1 to 10). Use a while loop when you repeat until some condition changes and you may not know the count in advance. Both can do the same job; for is just tidier when counting.What is the difference between while and do-while?
while loop checks the condition first, so it may run zero times. A do-while loop runs the body once first and then checks, so it always runs at least once. Use do-while when the action must happen at least one time.When should I use foreach?
foreach to go through every item of an array without managing an index yourself. It is the cleanest way to loop over arrays, and it works for both numbered and key-value (associative) arrays.What causes an infinite loop in PHP?
$i++). Always make sure something inside the loop moves it toward ending.What is the difference between break and continue?
break exits the loop completely and moves on. continue skips just the current turn and jumps to the next one. break stops everything; continue only skips one round.