Introduction

When using the break statement with nested loops, you can pass an optional numeric argument to indicate how many levels of nesting to break out of.

For example:

Demo

<?php
         // Break out of the inner loop when $units == 5
         for ( $tens = 0; $tens  <  10; $tens++ ) {
           for ( $units = 0; $units  <  10; $units++ ) {
             if ( $units == 5 ) break 1;
             echo $tens . $units . " \n ";
           }//from   w  ww .ja va 2 s  . co m
         }

         // Break out of the outer loop when $units == 5
         for ( $tens = 0; $tens  <  10; $tens++ ) {
           for ( $units = 0; $units  <  10; $units++ ) {
             if ( $units == 5 ) break 2;
             echo $tens . $units . " \n ";
           }
         }
?>

Result

Related Topic