Lesson 18 of 50 – PHP Break and Continue
36%

PHP Break and Continue

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.

1. What Is the Break Statement?

The break statement immediately terminates the current loop.

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) {
        break;
    }

    echo $i . "<br>";
}
?>
Output:
1
2
3
4

When $i becomes 5, the break statement stops the loop. Therefore, 5 to 10 are not printed.

2. Break in a While Loop

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++;
}
?>
Output:
1
2
3
4
5

The loop stops when $i reaches 6. Notice that the counter is incremented during each iteration that reaches $i++.

3. What Is the Continue Statement?

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>";
}
?>
Output:
1
2
4
5

When $i is 3, continue skips the echo statement for that iteration. The loop does not stop.

4. Print Even Numbers Using Continue

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>";
}
?>
Output:
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.

5. Break and Continue in an Array

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>";
}
?>
Output:
10
20

Example using continue:

<?php
$numbers = [10, 20, 30, 40, 50];

foreach ($numbers as $number) {
    if ($number == 30) {
        continue;
    }

    echo $number . "<br>";
}
?>
Output:
10
20
40
50
6. Break and Continue in Nested Loops

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>";
    }
}
?>
Output:
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.

7. Difference Between Break and Continue
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

8. Avoid Infinite Loops When Using Continue

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++;
}
?>
Output:
1
2
4
5

The counter is incremented before continue, so the loop progresses correctly.

9. Practical Example: Search for a Number

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!";
}
?>
Output:
Number found!

The loop stops as soon as the target number is found. This avoids unnecessary iterations.

Practice Assignments
  1. Print numbers from 1 to 20, but stop when the number reaches 12.
  2. Print numbers from 1 to 15, skipping number 7.
  3. Print only odd numbers from 1 to 20 using continue.
  4. Use foreach to search for a particular name in an array and stop when it is found.
  5. Use a while loop to print 1 to 10, skipping 5 without creating an infinite loop.
  6. Use nested loops to print a multiplication pattern and stop the inner loop at a chosen value.

🧠 Quick Quiz

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?