Sometimes you want to exit a loop immediately as soon as some condition is met. The break instruction exits a loop from any point in its block, bypassing the loop’s condition. Execution resumes at the next statement after the loop. You can use a break instruction with a while, do, or for loop.
This example finds the first value in a string array that is longer than 4 characters.
string $words[] = {"a","bb","ccc","dddd","eeeee","ffffff"};
string $long = "";
for ($i = 0; $i < size($words); $i++) {
if (size($words[$i]) > 4) {
$long = $words[$i];
break;
}
print($words[$i] + " is too short...\n");
};
print($long + " is the first long word.\n");