How to Break Foreach Loop in PHP?
Last Updated :
23 Jul, 2024
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:
Using break Keyword
In this approach, we are using the break keyword within a foreach loop in PHP to prematurely exit the loop when the condition $number === 3 is met. This makes sure that only the elements before the number 3 in the $numbers array are processed and printed.
Syntax:
break;
Example: The below example uses the break keyword to break foreach loop in PHP.
PHP
<?php
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . "<br>";
if ($number === 3) {
break;
}
}
?>
Output:
1
2
3
Using goto Keyword
In this approach, we are using the goto keyword within a foreach loop in PHP to jump to a labeled section called endloop when the condition $number === 3 is met. This breaks the loop and continues execution after the labeled section.
Syntax:
goto labelName;
Example: The below example uses the goto keyword to break foreach loop in PHP.
PHP
<?php
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . "<br>";
if ($number === 3) {
goto endloop;
}
}
endloop:
?>
Output:
1
2
3
Using array_filter FunctionAdded
In this approach, we use the array_filter function to filter the array elements before the foreach loop starts. By only including elements before the specified condition in the array, we can effectively break the loop indirectly.
Syntax:
array_filter(array $array, callable $callback)
Example: The below example uses the array_filter function to filter elements up to the number 3, thus breaking the foreach loop indirectly.
PHP
<?php
// Nikunj Sonigara
$numbers = [1, 2, 3, 4, 5];
$filteredNumbers = array_filter($numbers, function($number) {
return $number <= 3;
});
foreach ($filteredNumbers as $number) {
echo $number . "\n";
}
?>
Using array_walk with Exception Handling
Another approach to breaking a foreach loop in PHP is by using array_walk in combination with exception handling. This method involves throwing an exception when a certain condition is met within the array_walk callback, which effectively stops the iteration.
Example: In this example we will see how to use array_walk and exception handling to break a foreach loop in PHP.
PHP
<?php
function breakLoop($number) {
if ($number === 3) {
throw new Exception("Breaking the loop");
}
echo $number . "\n";
}
$numbers = [1, 2, 3, 4, 5];
try {
array_walk($numbers, 'breakLoop');
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
?>
Output1
2
Breaking the loop
Similar Reads
How to break forEach() method in Lodash ? The Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element. In this article, we will see how to break the forEach loop in ladash library. Syntax: _.forEach( collection, [iterate = _.identity] ) Parameters: This method accepts two parameters as mention
2 min read
How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs
3 min read
How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin
2 min read
How to parse a CSV File in PHP ? In this article, we learn to parse a CSV file using PHP code, along with understanding its basic implementation through examples. Â Approach: The following approach will be utilized to parse the CSV File using PHP, which is described below: Step 1. Add data to an Excel file. The following example is
3 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 nam
3 min read
How to Break a for loop When Response Code is 200? In the world of API testing, one frequent challenge is dealing with various response codes, such as the all-important HTTP status code 200 (OK). These codes provide critical information about the success or failure of an API request. In this guide, we will explore how to efficiently handle response
6 min read