Php 中断if语句并继续循环
如何在foreach和continue循环中中断if语句? 我用休息;但它也结束了循环:(Php 中断if语句并继续循环,php,Php,如何在foreach和continue循环中中断if语句? 我用休息;但它也结束了循环:( @奈杰尔·伦(Nigel Ren)已经对我要说的话发表了评论,但我还是要添加它作为答案。如果语句不需要中断或继续。请看以下示例: foreach(...) { if(condition) { //will work only if the condition is true } else { //will work if the condition is not true }
@奈杰尔·伦(Nigel Ren)已经对我要说的话发表了评论,但我还是要添加它作为答案。如果语句不需要
中断
或继续
。请看以下示例:
foreach(...) {
if(condition) {
//will work only if the condition is true
} else {
//will work if the condition is not true
}
//will always work (after if-else block) as long as the loop continues
}
只有当你真的想停止循环时,你才会中断 在您的情况下,我猜您希望循环到结束,因此不需要中断 当if/elseif无效时,可以使用中断 因此,您可以在if和elseif语句中添加continue,在最后一个elseif之后添加break(在if-elseif语句之外)
改为尝试
continue
。但是,在代码中不需要添加该选项。循环将在if语句之后继续。if语句不需要中断
,只需(在本例中)与匹配的close}
{
您还应该在循环之前完成2个Prepare,并在循环内绑定和执行。保存您自己的count($array)到数据库的不必要的往返次数反复编译相同的2个查询不必要的问题解决了很多
foreach(...) {
if(condition) {
//will work only if the condition is true
} else {
//will work if the condition is not true
}
//will always work (after if-else block) as long as the loop continues
}
foreach($arry as $s){
if(trim($row['img1']) == ''){
$insrt="UPDATE testt SET img1=:img1";
$r=$connect->prepare($insrt);
$r->bindparam(":img1",$s);
$r->execute();
echo"$s";
continue;
}elseif(trim($row['img2']) == ''){
$insrt="UPDATE testt SET img2=:img2";
$r=$connect->prepare($insrt);
$r->bindparam(":img2",$s);
$r->execute();
echo"$s";
continue;
}
break;
}