PHP break (Single and Nested Loops)
Last Updated :
20 Nov, 2018
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
// 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
// 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
Similar Reads
Nested Loops in Programming In programming, Nested Loops occur when one loop is placed inside another. These loops are quite useful in day-to-day programming to iterate over complex data structures with more than one dimension, such as a list of lists or a grid. In this article, we will learn about the basics of nested loops a
6 min read
Variables and Datatypes in PHP A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.$name = "GFG";$age = 30;Declaring Variables in PHPTo declare a variable, you simply use the dollar sign followed
4 min read
PHP while Loop The while loop is the simple loop that executes nested statements repeatedly while the expression value is true. The expression is checked every time at the beginning of the loop, and if the expression evaluates to true then the loop is executed otherwise loop is terminated. Flowchart of While Loop:
1 min read
How to parse and process HTML/XML using PHP ? In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is use
2 min read
How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or
3 min read
PHP Interview Questions and Answers (2024) | Set-2 In this article, you will learn PHP Interview Questions and Answers that are most frequently asked in interviews. Before proceeding to learn PHP Interview Questions and Answers Set 2, first we learn the complete PHP Tutorial and PHP Interview Questions and Answers. PHP Interview Questions and Answer
8 min read