start learning
Image 1
5141516

Break statement

The break statement is used to exit a loop or switch statement prematurely. When executed, it terminates the nearest enclosing loop or switch statement and continues executing the code immediately following the loop or switch.

Break in a for loop


for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        break; // Exit the loop when $i is equal to 3
    }
    echo $i . ' ';
}
 

In this example, we have a for loop that iterates from $i = 1 to $i = 5. Inside the loop, there is an if condition that checks if $i is equal to 3. When $i becomes 3, the break statement is executed, and the loop is terminated prematurely. As a result, only 1 and 2 are echoed to the screen.


Break in a while loop


$num = 1;
while ($num <= 5) {
    if ($num == 4) {
        break; // Exit the loop when $num is equal to 4
    }
    echo $num . ' ';
    $num++;
}
 

This example demonstrates the use of break in a while loop. The loop continues to execute as long as $num is less than or equal to 5. When $num reaches 4, the break statement is encountered, causing the loop to terminate.As a result, only 1, 2, and 3 are echoed to the screen.


Break in a switch statement


$day = 'Monday';

switch ($day) {
    case 'Monday':
    case 'Tuesday':
    case 'Wednesday':
    case 'Thursday':
    case 'Friday':
        echo 'Weekday';
        break; // Exit the switch statement
    case 'Saturday':
    case 'Sunday':
        echo 'Weekend';
        break; // Exit the switch statement
}
 

In this example, we use break within a switch statement to control the flow of the program based on the value of the $day variable. When $day matches one of the weekday cases (Monday through Friday), the associated message "Weekday" is echoed, and the break statement exits the switch statement. If $day were to match "Saturday" or "Sunday," the "Weekend" message would be echoed, and the break statement would also exit the switch statement.


break in a do-while loop


$num = 1;
do {
    echo $num . ' ';
    if ($num == 3) {
        break; // Exit the loop when $num is equal to 3
    }
    $num++;
} while ($num <= 5);
 

This example employs the break statement within a do-while loop. The loop initially prints the value of $num, and then it checks if $num is equal to 3. When $num becomes 3, the break statement is executed, causing the loop to exit. Consequently, only 1, 2, and 3 are echoed to the screen.


Break with nested loops


for ($i = 1; $i <= 3; $i++) {
    echo "Outer loop iteration $i: ";
    for ($j = 1; $j <= 3; $j++) {
        echo "$j ";
        if ($j == 2) {
            break; // Exit the inner loop when $j is equal to 2
        }
    }
    echo "<br>";
}
 

In this example, there are two nested for loops. The outer loop runs three times, and the inner loop runs three times within each outer loop iteration. However, when the inner loop reaches $j == 2, the break statement is executed, causing only the first two values from the inner loop to be printed.


Break with a labeled loop


$num = 1;
$found = false;

outerLoop:
while (!$found) {
    innerLoop:
    for ($i = 1; $i <= 3; $i++) {
        for ($j = 1; $j <= 3; $j++) {
            if ($j * $i === 6) {
                $found = true;
                break outerLoop; // Exit both loops labeled as outerLoop
            }
        }
    }
}
echo "The first product that equals 6 is $i * $j.";
 

This example demonstrates the use of a labeled loop with break. The outer loop has the label outerLoop, and the inner loops are labeled innerLoop. When the condition $j * $i === 6 is met, the $found variable is set to true, and the break outerLoop; statement is executed, causing both the outer and inner loops to exit. As a result, the program prints the message indicating that the first product equal to 6 is $i * $j.


These examples showcase various scenarios where the break statement can be used to control the flow of PHP code, whether it's within different types of loops, labeled loops, or within specific conditional checks.