Home >Backend Development >PHP Tutorial >php的continue使用简单案例

php的continue使用简单案例

WBOY
WBOYOriginal
2016-06-23 13:41:181026browse

continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。例如:

 <?php $i=0;     while($i<5)     {       if($i==2)        {          continue;        }       echo "$i<br>";       $i++;     }  ?>

当$i为2时,会跳出循环,执行下一次的循环。输出结果为:

0134
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是 1,即跳到当前循环末尾。

 <?phpfor ($m=0; $m <5; $m++){      for($i=0; $i <5; $i++)     {     	     	if($i > 3)     	{          continue 2;               }       echo "$i    $m    <br>";     }           }?>
结果为
0 0 1 0 2 0 3 0 0 1 1 1 2 1 3 1 0 2 1 2 2 2 3 2 0 3 1 3 2 3 3 3 0 4 1 4 2 4 3 4 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn