PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In PHP, ‘break‘ and ‘continue‘ are control flow statements used to alter the flow of loops such as ‘for‘, ‘foreach‘, ‘while‘, and ‘do-while‘. They provide mechanisms to manage how the loop execution proceeds under certain conditions.

'break'

The ‘break‘ statement is used to terminate the execution of the loop or switch statement it is placed within. When ‘break‘ is encountered, the program immediately exits the loop or switch, and control is transferred to the statement following the terminated loop or switch.

Usage in Loops

Here’s an example of ‘break‘ in a ‘for‘ loop:

				
					for ($i = 0; $i < 10; $i++) {
    if ($i == 5) {
        break; // Exit the loop when $i equals 5
    }
    echo $i . " ";
}

				
			

In this example, the loop will print ‘0 1 2 3 4‘ and then terminate when ‘$i‘ equals ‘5‘.

Usage in Switch Statements

Here’s an example of ‘break‘ in a ‘switch‘ statement:

				
					$day = "Wednesday";

switch ($day) {
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    case "Wednesday":
        echo "Today is Wednesday.";
        break;
    default:
        echo "Invalid day.";
        break;
}

				
			

In this example, the ‘break‘ statement ensures that only the matching case block executes. Without ‘break‘, PHP would continue executing the subsequent cases even if one case has already matched.

Breaking Out of Nested Loops

PHP allows you to break out of multiple nested loops by specifying an optional numeric argument to ‘break‘. The number indicates how many nested loops to break out of.

				
					for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($i == 1 && $j == 1) {
            break 2; // Exit both loops when $i equals 1 and $j equals 1
        }
        echo "i = $i, j = $j\n";
    }
}

				
			

In this example, both loops are terminated when ‘$i‘ equals ‘1‘ and ‘$j‘ equals ‘1‘.

'continue'

The ‘continue‘ statement is used to skip the rest of the code inside the current loop iteration and move to the next iteration of the loop.

Usage in Loops

Here’s an example of ‘continue‘ in a ‘for‘ loop:

				
					for ($i = 0; $i < 10; $i++) {
    if ($i == 5) {
        continue; // Skip the rest of the loop iteration when $i equals 5
    }
    echo $i . " ";
}

				
			

In this example, the loop will print ‘0 1 2 3 4 6 7 8 9‘, skipping the number 5.

Continuing Outer Loops in Nested Loops

Like ‘break‘, ‘continue‘ can also be used with an optional numeric argument to skip iterations of outer loops.

				
					for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($i == 1 && $j == 1) {
            continue 2; // Skip to the next iteration of the outer loop when $i equals 1 and $j equals 1
        }
        echo "i = $i, j = $j\n";
    }
}

				
			

In this example, when ‘$i‘ equals ‘1‘ and ‘$j‘ equals ‘1‘, the inner loop is skipped, and the next iteration of the outer loop begins.

  • break: Terminates the current loop or switch statement and transfers control to the statement following the loop or switch.
    • Can break out of multiple nested loops by specifying a numeric argument.
  • continue: Skips the remaining code in the current loop iteration and moves to the next iteration of the loop.
    • Can skip iterations of outer loops by specifying a numeric argument.

These control flow statements are powerful tools for managing loop execution and are essential for writing efficient and effective PHP code.

Scroll to Top