Skip to content

PHP Break Statement in Loops

Syntax

The break statement in PHP exits the current loop when a specific condition is met.

// Using break in a loop
foreach ($array as $value) {
if (condition) {
break; // Exit the loop
}
// Code to execute if condition is not met
}

Example

// Break out of a loop when a condition is met
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break; // Exit the loop when $i is 5
}
echo $i . "\n";
}
/* Output:
1
2
3
4
*/