PHP break and continue Statements (With Examples)

By Shahnail Khan Last updated : December 10, 2023

PHP offers various control structures to enhance the flow of your code. Two essential constructs for controlling loops are the break and continue statements. In this tutorial, we'll explore the functionalities of these statements and how they can be used effectively in your PHP code.

PHP break Statement

The break statement in PHP is used to terminate the execution of a loop. In simple words, we can say that when a break statement is added within a loop, the loop immediately exits, and the program continues with the next statement after the loop.

Example of break statement

Below is an example of PHP break statement.

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i == 5) {
        break;
    }
    echo $i . "<br>";
}
?>

The output of the above example is:

1
2
3
4

In this example, the loop will terminate when the value of $i becomes 5. So, it will print the first 4 numbers.

PHP continue Statement

On the other hand, the continue statement is used to skip the rest of the code inside a loop for the current iteration and move on to the next iteration.

Example of continue statement

Below is an example of PHP continue statement.

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue;
    }
    echo $i . " <br>";
}
?>

The output of the above example is:

1
2
4
5

In this example, when $i is equal to 3, the continue statement will skip the rest of the loop code for that iteration, resulting in the output 1 2 4 5.

Break vs Continue Statement in PHP

Below are the key differences between break and continue statements in PHP:

Feature break Statement continue Statement
Purpose Exits the current loop prematurely when a condition is met. Skips the rest of the code for the current iteration and moves to the next iteration.
Use Case Useful when you want to stop the loop once a specific condition is satisfied. Useful when you want to skip the remaining code for a particular iteration based on a condition.
Effect on Loop Terminates the entire loop immediately. Skips the rest of the code within the loop for the current iteration and moves to the next iteration.
Example
<?php
foreach ($array as $value) {
    if ($value === $target) {
        break;
    }
}
?>
<?php
foreach ($array as $value) {
    if ($value < 0) {
        continue;
    } // Process positive values
}
?>
Useful For Situations where you need to exit the loop early, such as finding a specific item. Skipping unnecessary processing for certain elements or conditions within the loop.
Loop Type Applicable to all types of loops (for, while, foreach). Applicable to all types of loops (for, while, foreach).
Multiple Loops Can be used to break out of nested loops. Breaks only the innermost loop when used within nested loops. To break out of multiple levels, labels may be required.

Examples on PHP break and Continue Statements

Now let's see how these statements work in different loops.

Example 1: Using break and continue in a for Loop

<?php
// Using break in a for loop
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        break;
    }
    echo $i . " ";
}
// Output: 1 2

// Using continue in a for loop
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue;
    }
    echo $i . " ";
}
// Output: 1 2 4 5
?>

In the first loop, when $i equals 3, the break statement stops the loop. So, it only prints 1 and 2. In the second loop, when $i is 3, the continue statement skips that part of the loop, so it prints 1, 2, 4, and 5.

Example 2: Using break and continue in a while Loop

<?php
// Using break in a while loop
$i = 1;
while ($i <= 5) {
    if ($i == 3) {
        break;
    }
    echo $i . " ";
    $i++;
}
// Output: 1 2

// Using continue in a while loop
$i = 1;
while ($i <= 5) {
    if ($i == 3) {
        $i++;
        continue;
    }
    echo $i . " ";
    $i++;
}
// Output: 1 2 4 5
?>

In the first while loop, it stops when $i is 3. In the second one, when $i is 3, continue skips the rest of the loop code for that iteration, so it prints 1, 2, 4, and 5.

Example 3: Using break and continue in a foreach Loop

<?php
// Using break in a foreach loop
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
    if ($number == 3) {
        break;
    }
    echo $number . " ";
}
// Output: 1 2

// Using continue in a foreach loop
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
    if ($number == 3) {
        continue;
    }
    echo $number . " ";
}
// Output: 1 2 4 5
?>

In the first foreach loop, it stops when it finds 3. In the second one, when it finds 3, continue skips that part of the loop, so it prints 1, 2, 4, and 5.

PHP practices based on break and continue statements

Below are the practices to understand the concept of PHP break and continue statements.

Practice 1

You are given an array of numbers. Write a PHP script to find the first occurrence of a number greater than 50 and stop the search.

Solution

<?php
$numbers = [20, 30, 45, 60, 25, 70, 55];

foreach ($numbers as $number) {
    if ($number > 50) {
        echo "The first number greater than 50 is: $number";
        break; // Stop the loop once the condition is met
    }
}
?>

The output is:

The first number greater than 50 is: 60

Practice 2

You have an array of integers. Write a PHP script to calculate the sum of positive numbers, skipping the negative ones.

Solution

<?php
$integers = [10, -5, 8, -3, 15, -7, 20];

$sum = 0;

foreach ($integers as $number) {
    if ($number < 0) {
        // Skip negative numbers
        continue;
    }
    // Add positive numbers to the sum
    $sum += $number;
}

echo "The sum of positive numbers is: $sum";
?>

The output is:

The sum of positive numbers is: 53

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.