Open In App

How to break an outer loop with PHP ?

Last Updated : 01 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Using break keyword: The break keyword is used to immediately terminate the loop and the program control resumes at the next statement following the loop. To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure. But in nested loops, to exit out from all or some of the outer loops, we need to pass a numeric argument which tells it how many nested enclosing structures are to be terminated.

Syntax:

break number_of_nested_loop_to_terminate;

Parameters: The break keyword followed by a numeric argument which is by default 1. Passing of variables and 0 as a numeric argument is not allowed.

Examples:

break 2; // It terminates second outer loop
break 3; // It terminates third outer loop
break $num; // Variable cannot be used as numeric argument since version 5.4.0
break 0; // 0 is not a valid argument to pass

Below programs illustrate how to break outer loops in PHP:

Program 1: PHP program to display the number while inner loop is not terminated.




<?php
  
// Initialize a number
$num = 10;
  
// Outer loop to iterate $num times
for( $i = 0; $i < 10; $i++ ) {
      
    // Initialize a variable $j
    $j = 1;
      
    // Inner loop
    while( $j <= 3 ) {
          
        if( $i >= 1 )
          
            // Breaking the outer loop
            break 2;
              
        echo $j . " ";
        $j++;
    
}
  
?>


Output:

1 2 3

Program 2: PHP program to search a number in an array and break the outer loop when it is found.




<?php
  
// Create a 2D array
$arr = array(
    array (105, 96, 112), 
    array(96, 45, 63) 
);
  
// Initialize a number which
// need to fine in the array
$num = 45;
  
// Declare a boolean variable and
// initialize it with false
$found = FALSE;
  
// Outer loop
for($i = 0; $i < 2; $i++) {
      
    // Inner loop
    for($j = 0; $j < 3; $j++) {
          
        if($num == $arr[$i][$j]) {
            $found = TRUE;
              
            // Terminate the outer loop
            break 2;
        }
    
}
  
// Check number is found or not
if( $found )
    echo $num . " is found in the array";
else
    echo $num . " is not found in the given array";
  
?>


Output:

45 is found in the array

Using goto keyword: The goto keyword is used to jump the section of the program. It jumps to the target label.

Program 3: PHP program to break the outer loop using goto keyword.




<?php
  
// Create a 2D array
$arr = array(
    array (105, 96, 112), 
    array(96, 45, 63) 
);
  
// Initialize a number which
// need to fine in the array
$num = 45;
  
// Declare a boolean variable and
// initialize it with false
$found = FALSE;
  
// Outer loop
for($i = 0; $i < 2; $i++) {
      
    // Inner loop
    for($j = 0; $j < 3; $j++) {
          
        if($num == $arr[$i][$j]) {
            $found = TRUE;
              
            // Terminate the outer loop
            // using goto keyword
            goto terminateLoop;
        }
    
}
  
// target label
terminateLoop:
  
// Check number is found or not
if( $found )
    echo $num . " is found in the array";
else
    echo $num . " is not found in the given array";
  
?>


Output:

45 is found in the array


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads