Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The break statement is a control flow structure used to prematurely terminate the execution of the current enclosing for, foreach, while, do-while, or switch block. When encountered, the PHP runtime immediately halts the iteration or evaluation of the current structure and transfers execution to the statement immediately following the terminated block.

Syntax

break;
// or
break [int];

Mechanics and Behavior

  • Execution Transfer: break unconditionally jumps out of the active loop or switch construct. It does not terminate the script, nor does it break out of standard conditional statements like if (unless the if statement is nested within a loop or switch). Note that because match is an expression and its arms strictly require expressions, it is syntactically impossible to place a break statement directly inside a match construct.
  • Nesting Level Argument: break accepts an optional positive integer argument that specifies how many nested enclosing structures are to be terminated. If omitted, the default value is 1, meaning it will only break out of the immediate enclosing structure.
  • Argument Constraints: The argument passed to break must be a constant integer literal (e.g., break 2;). Passing variables or dynamically evaluated expressions (e.g., break $levels;) is invalid and will result in a fatal error.

Syntax Visualization

Single-Level Break Terminates only the immediate enclosing structure.
while (true) {
    // Execution reaches here
    break; 
    // Execution never reaches here
}
// Control transfers here
Multi-Level Break Terminates multiple nested structures based on the provided integer literal.
for ($i = 0; $i < 10; $i++) {
    switch ($i) {
        case 5:
            // Terminates both the switch statement (level 1) 
            // and the enclosing for loop (level 2)
            break 2; 
    }
}
// Control transfers here after $i equals 5
Master PHP with Deep Grasping Methodology!Learn More