continue 2 and break in switch statement

前端 未结 2 1689
[愿得一人]
[愿得一人] 2021-02-11 15:03

I am new to PHP and saw the code below online. It has continue 2 and break together in switch/case statement. What does it mean?



        
相关标签:
2条回答
  • 2021-02-11 15:57

    continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

    Source: http://php.net/manual/en/control-structures.continue.php

    0 讨论(0)
  • 2021-02-11 16:04

    The continue 2 skips directly to the next iteration of the structure that is two levels back, which is the foreach. The break (equivalent to break 1) just ends the switch statement.

    The behavior in the code you've shown is:

    Loop through $elements. If an $element is type "a" and condition1 is met, or if it's type "b" and condition2 is met, skip to the next $element. Otherwise, perform some action before moving to the next $element.


    From PHP.net:continue:

    continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

    From PHP.net:switch

    PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.

    If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2.

    0 讨论(0)
提交回复
热议问题