Chapter 5: Loops in PHP
Learn about loops in PHP, including for
, while
, and foreach
loops, to automate repetitive tasks and process collections of data.
In this chapter, we’ll explore loops in PHP, which allow you to repeat a block of code multiple times. Loops are essential for automating repetitive tasks and processing collections of data, such as arrays.
The for
Loop
The for
loop is commonly used when you know in advance how many times you want to execute a block of code. Here’s the basic syntax:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration: " . $i . "<br>";
}
?>
In this example, the loop runs 5 times, incrementing $i
each time, and outputs "Iteration: 1" through "Iteration: 5".
The while
Loop
The while
loop executes a block of code as long as a specified condition is true. It’s useful when the number of iterations isn’t known in advance:
<?php
$count = 1;
while ($count <= 5) {
echo "Count: " . $count . "<br>";
$count++;
}
?>
In this example, the loop continues as long as $count
is less than or equal to 5, outputting "Count: 1" through "Count: 5".
The do...while
Loop
The do...while
loop is similar to the while
loop, but it guarantees that the code block executes at least once, even if the condition is false:
<?php
$count = 1;
do {
echo "Count: " . $count . "<br>";
$count++;
} while ($count <= 5);
?>
In this example, "Count: 1" through "Count: 5" are displayed. The loop always executes at least once, even if the condition is initially false.
The foreach
Loop
The foreach
loop is specifically designed for iterating over arrays. It makes it easy to access each element in a collection:
<?php
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo "Fruit: " . $fruit . "<br>";
}
?>
In this example, each item in the $fruits
array is displayed, outputting "Fruit: Apple", "Fruit: Banana", and "Fruit: Cherry".
Nested Loops
Loops can be nested within other loops. This is useful when working with multidimensional arrays or generating complex data:
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "i: " . $i . ", j: " . $j . "<br>";
}
}
?>
In this example, the outer loop runs 3 times, and the inner loop also runs 3 times for each iteration of the outer loop, producing a 3x3 grid of output values.
Loop Control: break
and continue
The break
statement exits a loop early, while continue
skips the current iteration and proceeds to the next one:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue; // Skip iteration 3
}
echo "Iteration: " . $i . "<br>";
if ($i == 4) {
break; // Stop the loop at iteration 4
}
}
?>
In this example, iteration 3 is skipped due to continue
, and the loop exits early at iteration 4 due to break
.
Summary and Next Steps
In this chapter, we covered the main types of loops in PHP: for
, while
, do...while
, and foreach
. We also learned about nested loops and how to control loops with break
and continue
. In the next chapter, we’ll explore functions and modular code to make your PHP applications more organized and reusable.