我想一个接一个地运行两个循环,但似乎第一个循环中的return
语句结束了脚本:
// this runs
for ($i = 0; $i < 3; $i++) {
try {
foo();
return true;
} catch (Exception $e) {
try {
bar();
} catch (Exception $e) {
return false;
}
}
}
// this doesn't
for ($i = 0; $i < 3; $i++) {
try {
foo();
return true;
} catch (Exception $e) {
try {
bar();
} catch (Exception $e) {
return false;
}
}
}
答案 0 :(得分:4)
你不能在循环中使用return
,它意味着函数/类方法。
您应该将return false
替换为break
,等效循环,将return true
替换为continue
。但是,正如John Conde注意到的那样,你可以在这个例子中留下continue
。
function loop
---------------------------
return true; continue;
return false; break;
答案 1 :(得分:1)
删除return true
并将其替换为空。
或者,使用continue
。它将从下一次迭代重新开始循环。
<强>更新强>
将return false
替换为评论中提到的break