gpt4 book ai didi

php - "break 2"是什么意思?

转载 作者:IT王子 更新时间:2023-10-28 23:58:09 29 4
gpt4 key购买 nike

我总是使用和看到只有“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 :

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com