PHP Manual
Prev | Chapter 11. Control Structures | Next

# `break`

`break` ends execution of the current `if`, `for`, `while`, or `switch` structure.

`break` accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

      1
      2 $i = 0;
      3 while ($i < 10) {
      4     if ($arr[$i] == "stop") {
      5         break;  /* You could also write 'break 1;' here. */
      6     }
      7     $i++;
      8 }
      9
     10 /* Using the optional argument. */
     11
     12 $i = 0;
     13 while ( ++$i ) {
     14     switch ( $i ) {
     15     case 5:
     16         echo "At 5<br>\n";
     17         break 1;  /* Exit only the switch. */
     18     case 10:
     19         echo "At 10; quitting<br>\n";
     20         break 2;  /* Exit the switch and the while. */
     21     default:
     22         break;
     23     }
     24 }
     25

Prev | Home | Next
`foreach` | Up | `continue`