About This Chapter
Loops in PHP – for, while, do…while, and foreach
Loops allow you to execute a block of code multiple times until a specific condition is met. In PHP, we have several types of loops: for, while, do…while, and foreach. Each of them is useful in different scenarios.
For loop
Most often used when the number of repetitions is known.
<?php
for ($i = 0; $i < 5; $i++) {
echo "Iteration: $i <br>";
}
While loop
Executes code as long as the condition is true.
<?php
$i = 0;
while ($i < 5) {
echo "Counter: $i <br>";
$i++;
}
Do…while loop
Executes the code at least once and then checks the condition.
<?php
$i = 0;
do {
echo "Counter: $i <br>";
$i++;
} while ($i < 5);
Foreach loop
Designed for iterating over arrays and objects.
<?php
$fruits = ["apple", "banana", "pear"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
$user = ["name" => "John", "age" => 25];
foreach ($user as $key => $value) {
echo "$key: $value<br>";
}
Break and continue statements
break
– stops the loopcontinue
– skips to the next iteration
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) continue; // skip 5
if ($i == 8) break; // stop at 8
echo $i . " ";
}
Best practices
- Avoid infinite loops – always make sure the condition will eventually be met.
- For large datasets, use
foreach
– it is the most readable. - Use
break
andcontinue
only when they truly improve logic clarity.
FAQ
When should I use for and when while?
for
is best when the number of iterations is known. while
is used when the number of repetitions is not predetermined.
What’s the difference between do…while and while?
do…while
guarantees at least one execution, whereas while
may not run at all if the condition is false initially.
Does foreach work only with arrays?
No – since PHP 5, foreach also works with objects that implement the Iterator
interface.
Chapter Lessons
The for Loop in PHP – Complete Lesson for Beginners
Learn how to use the for loop in PHP. Master loop syntax, counters, arrays, HTML generation, and best practices with practical examples.
The while Loop in PHP – Complete Lesson for Beginners
Learn how to use while and do-while loops in PHP. Master loop syntax, file reading, database iteration, and best practices with examples.
The do-while Loop in PHP – Complete Lesson for Beginners
Learn how to use the do-while loop in PHP. Master loop syntax, menu systems, input validation, and best practices with practical examples.
The foreach Loop in PHP – Complete Lesson for Beginners
Learn how to use the foreach loop in PHP. Master array iteration, key-value pairs, references, and best practices with practical examples.
Control Instructions in PHP: break and continue
Learn how to use break and continue in PHP loops and switch statements. Master flow control with practical examples and best practices.