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

标签 php break control-structure

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

相关文章:

python - python是否有更好的控制结构或其他功能来简化代码?

php - 如何使用 Zend_Db_Adapter 在每次调用 "query"时运行多个 SQL 查询?

php - 如何处理条件为 'IN' 的 PHP API POST select 语句?

java - 如何退出两个嵌套循环?

c++ - 如何退出嵌套循环

php - 这段PHP代码可以去掉for循环吗?

javascript - 将图像上传到 MySQL 数据库的正确方法是什么?

php - Mysql/PHP - 账户图表 - 父账户中的汇总

java - Java中的程序在中断后仍然循环

c++ - while 循环表达式