➤ The break statement can be used to jump out of a loop.
// This example jumps out of the loop when a is equal to 5:
for ($a = 0; $a <= 10; $a++) {
if ($a == 5) {
break;
}
echo "The number is: $a \n";
}
/* outputs: The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
*/
// We print the elements of the array until the value is equal to "Audi".
$cars = array("BMW","Porsche","Audi","Mercedes-Benz");
foreach ($cars as $val) {
if ($val == "Audi") {
break;
}
echo "$val \n";
}
/* outputs: BMW
Porsche
*/