Change language

How do I break an outer loop with PHP?

Syntax:
break number_of_nested_loop_to_terminate; 
Parameters:Keyword break followed by a numeric argument, by default 1. Passing variables and 0 as a numeric argument are 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 
The following programs show how to break external loops in PHP:Program 1:PHP program to display numbers until the inner loop completes.  
// Initialize the number $num = 10;  
// Outer loop to repeat $num times for ( $i = 0; $i < 10; $i ++) { // Initialize the $j variable $j = 1; // Inner loop while ( $j < = 3) { if ( $i > = 1) // break the outer loop break 2; echo $j . "" ; $j ++; } }  
?>
Exit:
1 2 3 
Program 2:PHP program to find a number in an array and break the outer loop when it is found.  
// Create a two-dimensional array $arr = array ( array (105, 96, 112), array (96, 45, 63) );  
// Initialize the number that
// need to be penalized in the array $num = 45;  
// Declare a boolean variable and
// initialize it to 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; // End the outer loop break 2; } } }  
// Check number found or not if ( $found ) echo $num . "is found in the array" ; else echo $num . "is not found in the given array" ;  
?>
Exit:
45 is found in the array 
Using the goto keyword: Thegoto keyword is used to go to a program section. It navigates to the target shortcut.Program 3:PHP program to break the outer loop using the goto keyword.  
// Create a two-dimensional array $arr = array ( array (105, 96, 112), array (96, 45, 63) );  
// Initialize the number that
// need to be penalized in the array $num = 45;  
// Declare a boolean variable and
// initialize it to 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; // End the outer loop // using the goto keyword goto terminateLoop; } } }  
// target label
terminateLoop:  
// Check number found or not if ( $found ) echo $num . "is found in the array" ; else echo $num . "is not found in the given array" ;  
?>
Exit:
45 is found in the array 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method