有时,您希望在满足某些条件时立即退出循环。break 指令从其块中的任意点退出循环,并忽略循环的条件。继续执行循环后的下一语句。您可以将 break 语句与 while、do 或 for 循环结合使用。
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");