“中断2”的含义是什么?

2022-08-30 14:57:01

我总是使用和看到只有“休息”的例子。这是什么意思:

 <?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 
      } 
    } 
    ?>

“中断”语句是否有更多可用选项?


答案 1

休息的PHP文档

break 接受一个可选的数字参数,该参数告诉它要分解多少个嵌套的封闭结构。

如注释中所述,它打破了开关和同时

以下示例将中断所有循环:foreach

foreach (...) {
  foreach (..) {
    foreach (...) {
      if ($condition) {
        break 3;
      }
    }
  }
}

答案 2

推荐