The Wayback Machine - https://web.archive.org/web/20240717024439/https://www.geeksforgeeks.org/php-break-single-and-nested-loops/
Open In App

PHP break (Single and Nested Loops)

Last Updated : 20 Nov, 2018
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In PHP break is used to immediately terminate the loop and the program control resumes at the next statement following the loop.
Method 1: Given an array the task is to run a loop and display all the values in array and terminate the loop when encounter 5.

Examples:

Input : array1 = array( 1, 2, 3, 4, 5, 6, 7 )
Output : 1 2 3 4
         Loop Terminated
The loop contains an if condition and when condition is true then
loop will break otherwise display the array content. 

Input : array1 = array( '10', '2', '5', '20', '40' )
Output : 10 2
        Loop Terminated

Program:




<?php
// PHP program to break the loop
  
// Declare an array and initialize it
$array = array( 1, 2, 3, 4, 5, 6, 7 );
  
// Use foreach loop
foreach ($array as $a) {
    if ($a == 5)
        break;
    else
        echo $a . " ";
}
echo "\n";
echo "Loop Terminated";
?>


Output:

1 2 3 4 
Loop Terminated

Method 2: Given nested loops, in PHP we can use break 2 to terminate two loops as well. Below program contains the nested loop and terminate it using break statement.
For example given two array arr1 and arr2, and the task is to display all the value of arr2 for every value of arr1 till the value of arr1 not equal to arr2. If the value in arr1 is equal to the value of arr2 then terminate both the loops using break 2 and execute the further statements.

Examples:

Input : arr1 = array( 'A', 'B', 'C' );
        arr2 = array( 'C', 'A', 'B', 'D' );
Output : A C 
         Loop Terminated

Input : arr1 = array( 10, 2, 5, 20, 40 )
        arr2 = array( 1, 2 )
Output :10 1 2
        2 1
        Loop Terminated




<?php
// PHP program to break the loop
  
// Declare two array and initialize it
$arr1 = array( 'A', 'B', 'C' );
$arr2 = array( 'C', 'A', 'B', 'D' );
  
// Use foreach loop
foreach ($arr1 as $a) {
    echo "$a ";
      
    // Ue nested loop
    foreach ($arr2 as $b) {
        if ($a != $b )
            echo "$b ";
        else
            break 2; 
    }
    echo "\n";
}
  
echo "\nLoop Terminated";
?>


Output:

A C 
Loop Terminated


Previous Article
Next Article

Similar Reads

What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. Difference between the "word-break: break-all;" and "word-wrap: break-word;" word-break: break-all; It is used to break the words at any ch
2 min read
How to break nested for loop using JavaScript?
The break statement, which is used to exit a loop early. A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code. Note: there should not be any other statement in between a label name and associated loop. Example-1: Break from neste
3 min read
How to set a single line break in HTML5 ?
In this article, we will add a single line break using the &lt;br&gt; tag. It s used to give the single line break. It is the empty tag so it does not contain end tag. Syntax: &lt;br&gt; Example 1: This example uses &lt;br&gt; tag to break the line. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; How to defines a single line break?
1 min read
PHP | Loops
Like any other language, loop in PHP is used to execute a statement or a block of statements, multiple times until and unless a specific condition is met. This helps the user to save both time and effort of writing the same code multiple times. PHP supports four types of looping techniques; for loop while loop do-while loop foreach loop Let us now
4 min read
How to Break Foreach Loop in PHP?
Breaking a foreach loop consists of terminating the execution of the loop prematurely based on a certain condition. In this article, we will explore and learn two approaches to breaking for each loop in PHP. Below are the approaches to break for each loop in PHP: Table of Content Using break KeywordUsing goto KeywordUsing array_filter FunctionAdded
3 min read
How to insert a line break in PHP string ?
In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed. Syntax: nl2br("string \n");where the string is the input string. Example 1: PHP Program to insert a line break in a string. [GFGTABS] PHP &lt;?php // Inser
2 min read
Alternatives of for loops and if-else blocks in ReactJS
Implementing loops in React generally gives the error stating Unexpected token and the same error shows up if you use if conditions as well. There we have alternatives to be used in JSX in react. Syntax for Loop in React: function App() { return ( &lt;div&gt; {for(let i =0 ; i&lt; 5 ; i++) &lt;/Component&gt;} &lt;/div&gt; } Explanation: Browser doe
3 min read
PHP - MySQL : Nested Query
In this article, we are going to perform nested query operations on the database in the MySQL server using the Xampp server. Introduction : PHP stands for hypertext preprocessor, which is a server-side scripting language and also used to handle database operations. We are a PHP xampp server to communicate with the database. The language used is MyS
5 min read
JavaScript Break and Continue
Break statementThe break statement is used to jump out of a loop. It can be used to "jump out" of a switch() statement. It breaks the loop and continues executing the code after the loop. Example: This example shows the implementation of the JavaScript Break statement. [GFGTABS] JavaScript let content = &quot;&quot;; let i; for (i = 1; i &lt; 1000;
3 min read
Difference Between label and break in JavaScript
The labels and the break statement are used within loops and control flow structures to enhance flexibility and manageability. Each serves distinct purposes and can be applied in the various scenarios based on the needs of the code. These are the following topics that we are going to discuss: Table of Content What is a Label?What is break?Differenc
3 min read
three90RightbarBannerImg