在 php 中不继续声明?
don't work continue statement in php?
我使用了一些代码来学习 break 和 continue 语句。 break 语句工作正常但 continue 语句不起作用。我会给出我的编码
<?php
for($a=1; $a<=10; $a++){
echo $a;
echo "<br>";
if($a==6){
break;
}
else{
continue;
}
}
continue
表示 "skip the rest of the loop and go back to the top of the loop",因为你的 continue
是你循环中的最后一件事,没有什么可以跳过,所以同样的事情会发生,无论 continue
在那里。
您的变量没有命中 continue
语句。看这个例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
输出将是:
9 7 6
因为在您的 for
循环中,continue
是最后一条语句,所以没有任何内容可以跳过,因为它会自动转到下一次迭代的开始。
continue is used within looping structures to skip the rest of the
current loop iteration and continue execution at the condition
evaluation and then the beginning of the next iteration
break ends execution of the current for, foreach, while, do-while or
switch structure.
for($a=1; $a<=10; $a++){<--------------------┐
|
echo $a; |
echo "<br>"; |
if($a==6){ |
break; ----- jumps here ------┐ |
} | |
| |
Remove else `continue` here,it will go | |
to the beginning automatically until | |
loop fails -----------------------------------┘
|
} |
<--------------------┘
根据评论:
<?php
for($a=1; $a<=10; $a++){
echo $a;
echo "<br>";
if($a==6){
break;
}
else{
echo "before continue <br/>";
continue;
echo "after continue <br/>"; // this will not execute because continue goes beginning of the next iteration
}
}
我使用了一些代码来学习 break 和 continue 语句。 break 语句工作正常但 continue 语句不起作用。我会给出我的编码
<?php
for($a=1; $a<=10; $a++){
echo $a;
echo "<br>";
if($a==6){
break;
}
else{
continue;
}
}
continue
表示 "skip the rest of the loop and go back to the top of the loop",因为你的 continue
是你循环中的最后一件事,没有什么可以跳过,所以同样的事情会发生,无论 continue
在那里。
您的变量没有命中 continue
语句。看这个例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
输出将是:
9 7 6
因为在您的 for
循环中,continue
是最后一条语句,所以没有任何内容可以跳过,因为它会自动转到下一次迭代的开始。
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration
break ends execution of the current for, foreach, while, do-while or switch structure.
for($a=1; $a<=10; $a++){<--------------------┐
|
echo $a; |
echo "<br>"; |
if($a==6){ |
break; ----- jumps here ------┐ |
} | |
| |
Remove else `continue` here,it will go | |
to the beginning automatically until | |
loop fails -----------------------------------┘
|
} |
<--------------------┘
根据评论:
<?php
for($a=1; $a<=10; $a++){
echo $a;
echo "<br>";
if($a==6){
break;
}
else{
echo "before continue <br/>";
continue;
echo "after continue <br/>"; // this will not execute because continue goes beginning of the next iteration
}
}