The continue and break statements in PHP allow developers to control loop execution. break exits the loop immediately, while continue skips the current iteration and proceeds with the next cycle.
for, while, and foreach loopsTarget Condition: When Number == 3
// Terminates the loop immediately
break;
// Skips the current iteration and continues
continue;
// Loop stops when value reaches 3
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo "Number: $i
";
}
?>
The loop prints numbers 1 and 2 only. When $i becomes 3, the break statement stops the loop entirely.
// Skips printing when value equals 3
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo "Number: $i
";
}
?>
The loop skips number 3 but continues execution, resulting in output: 1, 2, 4, and 5.
Think of break as an emergency exit ? from a loop, while continue is like skipping one step ⏭️ and moving ahead.
break to stop loops as soon as the goal is achievedcontinue to skip unnecessary processingbreak_continue.phpcontinuebreak