In PHP, break and continue
are used to control the execution of loops.
The break statement stops a loop, while
continue skips the remaining statements in the
current iteration and moves to the next iteration.
The break statement immediately terminates
the current loop.
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i . "<br>";
}
?>
1
2
3
4
When $i becomes 5, the break statement
stops the loop. Therefore, 5 to 10 are not printed.
We can also use break inside a while loop to stop repetition when a condition becomes true.
<?php
$i = 1;
while ($i <= 10) {
if ($i == 6) {
break;
}
echo $i . "<br>";
$i++;
}
?>
1
2
3
4
5
The loop stops when $i reaches 6.
Notice that the counter is incremented during each
iteration that reaches $i++.
The continue statement skips the remaining
statements in the current loop iteration. The loop
then proceeds to its next iteration.
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . "<br>";
}
?>
1
2
4
5
When $i is 3, continue skips the echo
statement for that iteration. The loop does not stop.
We can use continue to skip odd numbers and print only even numbers.
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 != 0) {
continue;
}
echo $i . "<br>";
}
?>
2
4
6
8
10
The expression $i % 2 gives the remainder
after division by 2. Odd numbers have a remainder
other than zero, so they are skipped.
Break and continue can also be used inside a foreach loop to control array processing.
Example using break:
<?php
$numbers = [10, 20, 30, 40, 50];
foreach ($numbers as $number) {
if ($number == 30) {
break;
}
echo $number . "<br>";
}
?>
10
20
Example using continue:
<?php
$numbers = [10, 20, 30, 40, 50];
foreach ($numbers as $number) {
if ($number == 30) {
continue;
}
echo $number . "<br>";
}
?>
10
20
40
50
When using nested loops, an ordinary break or continue affects the innermost loop in which it appears.
Example using break:
<?php
for ($i = 1; $i <= 3; $i++) {
echo "Outer loop: $i<br>";
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
break;
}
echo "Inner loop: $j<br>";
}
}
?>
Outer loop: 1
Inner loop: 1
Outer loop: 2
Inner loop: 1
Outer loop: 3
Inner loop: 1
The inner loop stops whenever $j reaches 2,
but the outer loop continues.
PHP also supports break 2; to exit two
nested loop levels and continue 2; to
continue at the next iteration of the second enclosing
loop level.
| Break | Continue |
|---|---|
| Terminates the current loop. | Skips the current iteration. |
| No further iterations of that loop occur. | The loop proceeds to its next iteration. |
| Useful when a target is found. | Useful when certain values should be skipped. |
| Can be used in loops and switch statements. | Primarily used to skip loop iterations. |
Break example:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo $i . " ";
}
?>
Output: 1 2
Continue example:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . " ";
}
?>
Output: 1 2 4 5
Be careful when using continue inside a while loop. If the counter update is skipped, the loop may never reach its stopping condition.
Incorrect example:
<?php
$i = 1;
while ($i <= 5) {
if ($i == 3) {
continue;
}
echo $i . "<br>";
$i++;
}
?>
Why is it incorrect? When
$i becomes 3, continue skips
$i++. The value stays 3, so the loop
repeats indefinitely.
Correct example:
<?php
$i = 1;
while ($i <= 5) {
if ($i == 3) {
$i++;
continue;
}
echo $i . "<br>";
$i++;
}
?>
1
2
4
5
The counter is incremented before continue, so the loop progresses correctly.
Suppose we want to search for a number in an array. Once the number is found, we can use break to stop searching.
<?php
$numbers = [12, 25, 38, 45, 60];
$search = 38;
$found = false;
foreach ($numbers as $number) {
if ($number == $search) {
$found = true;
break;
}
}
if ($found) {
echo "Number found!";
} else {
echo "Number not found!";
}
?>
Number found!
The loop stops as soon as the target number is found. This avoids unnecessary iterations.
1. What does the break statement do?
2. What happens when continue executes inside a loop?
3. What can cause an infinite while loop when using continue?