PHP中break语句的应用

布满荆棘的人生 2022-05-11 22:36 265阅读 0赞

一 实例

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>使用break关键字跳出多重循环</title>
  6. <style type="text/css">
  7. <!--
  8. body {
  9. background-color: #CCFF00;
  10. }
  11. -->
  12. </style></head>
  13. <body>
  14. <?php
  15. while(true){
  16. for(;;){
  17. for($i=1;$i<=4;$i++){
  18. echo $i." <img src='images/$i.jpg'>"."\t";
  19. if($i == 3){
  20. echo "<p>变量\$i等于3,跳出一重循环。<p>";
  21. break 1;
  22. }
  23. }
  24. for($j = 1; $j < 5; $j++){
  25. echo $j." <img src='images/$j.jpg'>"."\t";
  26. if($j == 4){
  27. echo "<p>变量\$j等于4,跳出最外重循环。";
  28. break 3;
  29. }
  30. }
  31. }
  32. echo "看不到我吧,呵呵!";
  33. }
  34. ?>
  35. </body>
  36. </html>

二 运行结果

c21f7e12-f5d2-3461-91c6-d8c52337a8ad.png

三 注意事项

本实例共3层循环,最外层的while循环和中间的for循环是无限循环,最里面并列两个for循环:程序首先执行第1个循环,循环3次,跳出当前循环(一重循环),然后继续执行第二个for循环,循环4次,然后跳出最外层的循环(体会break后的数字的含义,跳出针对自己的第3层循环)。

发表评论

表情:
评论列表 (有 0 条评论,265人围观)

还没有评论,来说两句吧...

相关阅读