我经常使用和看到带有“break”的示例。这是什么意思:
<?php
while ($flavor = "chocolate") {
switch ($flavor) {
case "strawberry";
echo "Strawberry is stock!";
break 2; // Exits the switch and the while
case "vanilla";
echo "Vanilla is in stock!";
break 2; // Exits the switch and the while
case "chocolate";
echo "Chocolate is in stock!";
break 2; // Exits the switch and the while
default;
echo "Sorry $flavor is not in stock";
break 2; // Exits the switch and the while
}
}
?>
'break'语句是否还有更多可用选项?
最佳答案
从PHP docs on break
:
如注释中所指出的,它脱离了开关。
以下示例将打破所有foreach
循环:
foreach (...) {
foreach (..) {
foreach (...) {
if ($condition) {
break 3;
}
}
}
}