您可以“退出" PHP中的循环吗? [英] Can you 'exit' a loop in PHP?
本文介绍了您可以“退出" PHP中的循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个循环正在我的PHP代码中进行一些错误检查.原来看起来像这样...
I have a loop that is doing some error checking in my PHP code. Originally it looked something like this...
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?
推荐答案
您正在寻找 break 声明.
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
这篇关于您可以“退出" PHP中的循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文