你能在 PHP 中“退出”循环吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588892/
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
Can you 'exit' a loop in PHP?
提问by alex
I have a loop that is doing some error checking in my PHP code. Originally it looked something like this...
我有一个循环在我的 PHP 代码中进行一些错误检查。原本看起来是这样的……
foreach($results as $result) {
if (!$condition) {
$halt = true;
ErrorHandler::addErrorToStack('Unexpected result.');
}
doSomething();
}
if (!$halt) {
// do what I want cos I know there was no error
}
This works all well and good, but it is still looping through despite after one error it needn't. Is there a way to escape the loop?
这一切都很好,但它仍然在循环,尽管发生了一个不需要的错误。有没有办法摆脱循环?
回答by TheTXI
回答by William Holroyd
As stated in other posts, you can use the break keyword. One thing that was hinted at but not explained is that the keyword can take a numeric value to tell PHP how many levels to break from.
如其他帖子所述,您可以使用 break 关键字。暗示但没有解释的一件事是,关键字可以采用一个数值来告诉 PHP 要突破多少级。
For example, if you have three foreach loops nested in each other trying to find a piece of information, you could do 'break 3' to get out of all three nested loops. This will work for the 'for', 'foreach', 'while', 'do-while', or 'switch' structures.
例如,如果您有三个相互嵌套的 foreach 循环试图查找一条信息,则可以执行 'break 3' 以退出所有三个嵌套循环。这将适用于“for”、“foreach”、“while”、“do-while”或“switch”结构。
$person = "Rasmus Lerdorf";
$found = false;
foreach($organization as $oKey=>$department)
{
foreach($department as $dKey=>$group)
{
foreach($group as $gKey=>$employee)
{
if ($employee['fullname'] == $person)
{
$found = true;
break 3;
}
} // group
} // department
} // organization
回答by Hans
break;leaves your loop.
break;离开你的循环。
continue;skips any code for the remainder of that loop and goes on to the next loop, so long as the condition is still true.
continue;跳过该循环剩余部分的任何代码并继续下一个循环,只要条件仍然为真。
回答by Patrick Michaelsen
All of these are good answers, but I would like to suggest one more that I feel is a better code standard. You may choose to use a flag in the loop condition that indicates whether or not to continue looping and avoid using breakall together.
所有这些都是很好的答案,但我想再推荐一个我认为更好的代码标准。您可以选择在循环条件中使用一个标志来指示是否继续循环并避免break一起使用。
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
$length = count($arr);
$found = false;
for ($i = 0; $i < $length && !$found; $i++) {
$val = $arr[$i];
if ($val == 'stop') {
$found = true; // this will cause the code to
// stop looping the next time
// the condition is checked
}
echo "$val<br />\n";
}
I consider this to be better code practice because it does not rely on the scope that breakis used. Rather, you define a variable that indicates whether or not to break a specific loop. This is useful when you have many loops that may or may not be nested or sequential.
我认为这是更好的代码实践,因为它不依赖于使用的范围break。相反,您定义一个变量来指示是否中断特定循环。当您有许多可能是也可能不是嵌套或顺序的循环时,这很有用。
回答by Piseth Sok
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
回答by prashant pandey
I have tried solutions with CSS but none of them worked for me.
我尝试过使用 CSS 的解决方案,但没有一个对我有用。
Replacing href="#"to href="javascript:void(0);"has worked as in this case modal doesn't jump to top and stays at the event element position.
更换href="#"到href="javascript:void(0);"曾担任在这种情况下,模式不跳转到顶部,并停留在事件元素的位置。

