我总是使用和看到只有“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' 语句是否有更多可用选项?
最佳答案
break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
正如评论中所指出的,它打破了 switch 和 while。
以下示例将跳出所有 foreach
循环:
foreach (...) {
foreach (..) {
foreach (...) {
if ($condition) {
break 3;
}
}
}
}
关于php - "break 2"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552721/