php 继续 2 并中断 switch 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26431066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
continue 2 and break in switch statement
提问by typeof programmer
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?
我是 PHP 新手,在网上看到了下面的代码。它有continue 2
和break
在一起的switch/case
声明。这是什么意思?
foreach ( $elements as &$element ) {
switch ($element['type']) {
case a :
if (condition1)
continue 2;
break;
case b :
if (condition2)
continue 2;
break;
}
// remaining code here, inside loop but outside switch statement
}
回答by showdev
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.
的continue 2
直接跳到结构,它是两级背,这是的下一次迭代foreach
。在break
(相当于break 1
)刚刚结束的switch
声明。
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
.
循环通过$elements
。如果 an$element
是类型“a”并且condition1
满足,或者如果它是类型“b”并且condition2
满足,则跳到下一个$element
。否则,在移动到下一个之前执行一些操作$element
。
From PHP.net:continue:
continueaccepts 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.
continue接受一个可选的数字参数,它告诉它应该跳到结束的封闭循环的级别。默认值为 1,因此跳到当前循环的末尾。
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.
PHP 会继续执行语句,直到 switch 块结束,或者第一次看到 break 语句。
If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2.
如果您在循环内有一个 switch 并且希望继续到外循环的下一次迭代,请使用 continue 2。
回答by Apul Gupta
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.
continue 接受一个可选的数字参数,它告诉它应该跳到结束的封闭循环的级别。默认值为 1,因此跳到当前循环的末尾。
Source: http://php.net/manual/en/control-structures.continue.php
来源:http: //php.net/manual/en/control-structures.continue.php
回答by Edward Manda
continue and break are similar in that the will stop something from happening.
continue 和 break 的相似之处在于将阻止某些事情发生。
in case of continue, it will stop anything after the braces but won't stop the loop. The switch statement just gets out of this statement and goes on to the next statement.
在继续的情况下,它会在大括号之后停止任何事情,但不会停止循环。switch 语句只是从这个语句中退出并继续下一个语句。
In case of break it will stop the entire loop from continuing, end the loop there.
在中断的情况下,它将停止整个循环继续,在那里结束循环。