Break Statement
Part of the Fundamentals section of Coddy's PHP journey — lesson 51 of 71.
Sometimes you need to exit a loop early, before it naturally finishes. The break statement lets you do exactly that—it immediately stops the loop and continues with the code after it.
Imagine searching through an array for a specific value. Once you find it, there's no point in checking the remaining elements. Using break saves unnecessary iterations.
<?php
$numbers = [3, 7, 2, 9, 5, 1];
foreach ($numbers as $num) {
if ($num === 9) {
echo "Found 9!\n";
break;
}
echo "Checking $num\n";
}
echo "Search complete";
?>This outputs:
Checking 3
Checking 7
Checking 2
Found 9!
Search completeNotice that 5 and 1 are never checked. The moment the loop encounters 9, break exits the loop entirely, and execution jumps to "Search complete".
The break statement works with all loop types—for, while, and foreach. It's particularly useful when you're searching for something or when a certain condition means the loop should stop immediately.
Challenge
EasyRead two lines of input:
- A comma-separated list of numbers (e.g.,
4,8,15,16,23,42) - A target number to search for
Use a foreach loop to search through the array for the target number. When you find it, print Found [target] at index [index]! and use break to exit the loop immediately.
If the loop completes without finding the target, print [target] not found.
Hint: Use the key-value syntax in your foreach loop to track the index.
Example 1:
If the inputs are 10,25,30,45,50 and 30, the output should be:
Found 30 at index 2!Example 2:
If the inputs are 5,10,15,20,25 and 20, the output should be:
Found 20 at index 3!Example 3:
If the inputs are 1,2,3,4,5 and 99, the output should be:
99 not foundCheat sheet
The break statement immediately exits a loop and continues execution with the code after the loop. It works with for, while, and foreach loops.
Example of using break to exit a loop early:
<?php
$numbers = [3, 7, 2, 9, 5, 1];
foreach ($numbers as $num) {
if ($num === 9) {
echo "Found 9!\n";
break;
}
echo "Checking $num\n";
}
echo "Search complete";
?>Output:
Checking 3
Checking 7
Checking 2
Found 9!
Search completeOnce break is executed, the loop stops immediately and remaining elements are not processed.
Try it yourself
<?php
// Read input
$numbers = explode(',', trim(fgets(STDIN)));
$target = trim(fgets(STDIN));
// TODO: Write your code below
// Use a foreach loop with key-value syntax to search for the target
// When found, print "Found [target] at index [index]!" and break
// If not found after the loop, print "[target] not found"
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators