The for loop, break and continue
In the previous lesson the counter lived in three places: the start value before while, the condition in parentheses and the step $i++ at the end of the body. A for loop gathers all three parts into one line.
On the running track, these are the start line, the checkpoint arch before each lap and the mark on the counter after the lap. In the for header they stand side by side, separated by semicolons:
<?php
$total = 0;
for ($i = 1; $i <= 4; $i++) {
$total += $i;
}
echo $total, "\n"; // 10
echo $i, "\n"; // 5The three parts of the header
The header for ($i = 1; $i <= 4; $i++) has three parts:
$i = 1runs once, before the first iteration — the runner steps onto the start line;$i <= 4is checked before each iteration — this is the checkpoint arch;$i++runs after each iteration — this is the mark on the counter.
Let's walk through the program step by step:
$total = 0;sets up the sum.- The loop runs iterations with
$iequal to1,2,3and4and adds them up:$totalbecomes10. - After the fourth iteration, the step makes
$iequal to5, the condition5 <= 4is false, and the loop ends. - The two
echostatements print10and5.
The variable $i does not disappear after the loop: in PHP it keeps the value that made the condition false. for is the choice when you know in advance where to count from and to, and while when the number of iterations depends on what happens in the body.
Two ways to cut an iteration short
Sometimes the runner leaves the track early or cuts the rest of a lap. A loop has two statements for this. Both are written on a line of their own, most often inside an if.
break
break ends the whole loop at once, and the program continues from the line after the loop. This loop looks for the first number greater than 50 that is divisible by 7:
for ($i = 51; $i <= 100; $i++) {
if ($i % 7 === 0) {
break;
}
}
echo $i, "\n"; // 56For the numbers from 51 to 55 the remainder is not zero, and the loop goes on. At 56 the break fires: the runner leaves the track, and the step $i++ no longer runs. That is why $i keeps the number that was found.
break sits inside the if, but it leaves the loop. An if is not a loop, so break ends the nearest loop or switch around it.
continue
continue skips only the rest of the current iteration. The runner cuts the rest of the lap but still makes the mark on the counter: after continue, the step $i++ runs, and then the condition is checked.
for ($i = 1; $i <= 7; $i++) {
if ($i % 3 === 0) {
continue;
}
echo $i; // 12457
}
echo "\n";For 3 and 6 the echo is never reached, and the other numbers are printed in a row: 12457. The loop still runs to the end, up to 7.
Where continue and break jump
Let's put both statements into one program. This is where people go wrong most often: where continue jumps and what $i holds after break. Press "Next step" and watch $i in the {main} frame on the iterations where continue and break fire.
1<?php2 3$total = 0;4for ($i = 1; $i <= 5; $i++) {5 if ($i === 3) {6 continue;7 }8 if ($i === 5) {9 break;10 }11 $total += $i;12}13echo $total, ' ', $i, "\n";
$total = 0 sets up the sum.
The rule the widget shows:
continuejumps to the step$i++, and then to the condition;breakleaves the loop at once, and the step no longer runs;- after
break,$ikeeps the value of the iteration on which the loop was cut short.
If loops are nested one inside another, break leaves only the inner one, and break 2 leaves both at once.
Here the picture with the mark holds only for for. In while the step $i++ sits in the body, and a continue written before it jumps over the step as well. Then $i does not change, and the loop repeats the same iteration forever.
Common mistake
A semicolon was put right after the for header:
for ($i = 1; $i <= 3; $i++); { // ✗
echo $i, "\n";
}The program prints a single line, 4, and PHP gives no message. The semicolon here is an empty body: the loop runs three empty laps, and the block in curly braces runs once, after the loop. By then $i holds 4. Remove the ;, and the loop prints 1, 2 and 3.
Key points
- The
forheader has three parts: the start runs once, the condition before each iteration, the step after each iteration. continueskips the rest of the iteration, and then the step and the condition check run.breakends the loop at once, and the step$i++no longer runs.- After the loop,
$iholds its last value: the one that made the condition false or the one at whichbreakfired.