Rumah >pembangunan bahagian belakang >tutorial php >php中break与continue的区别
break语句用在循环体或switch当中,意思是跳出当前循环。如果break出现在switch中是跳出switch当前switch语句体。在for循环中是跳出本层循环。而continue是本次循环,跳过本次循环后面的语句,继续下一次循环。
Eg:99乘法表
<code><span>for</span>(<span>$i</span>=<span>1</span>;<span>$i</span>10;<span>$i</span>++){ <span>for</span>(<span>$j</span>=<span>1</span>;<span>$j</span>$i;<span>$j</span>++){ <span>if</span>(<span>$j</span>==<span>3</span>) <span>//会跳过j=3的算式</span><span>continue</span>; <span>echo</span><span>$i</span>.<span>'x'</span>.<span>$j</span>.<span>'='</span>.<span>$i</span>*<span>$j</span>.<span>' '</span>; } <span>echo</span><span>"<br>"</span>; }</code>
巡行结果:当j=3的时候,会结束本次循环,继续下次循环,不会出现i*3这一列
<code><span>for</span>(<span>$i</span>=<span>1</span>;<span>$i</span>10;<span>$i</span>++){ <span>for</span>(<span>$j</span>=<span>1</span>;<span>$j</span>$i;<span>$j</span>++){ <span>if</span>(<span>$j</span>==<span>3</span>) <span>break</span>; <span>echo</span><span>$i</span>.<span>'x'</span>.<span>$j</span>.<span>'='</span>.<span>$i</span>*<span>$j</span>.<span>' '</span>; } <span>echo</span><span>"<br>"</span>; }</code>
结果:当循环到j=3的时候,会跳出本层循环,i*3和这一列以后的都不会执行,所以结果只会出现i*1和i*2这两列
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了php中break与continue的区别,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。