Blogger Information
Blog 26
fans 2
comment 0
visits 24413
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分支与循环
leverWang
Original
658 people have browsed it

一、分支,流程控制

  1. //1.单分支
  2. $num=10;
  3. $num1=20;
  4. if($num>$num1){
  5. echo 'num大于num1';
  6. }
  7. //2.双分支
  8. if($num<$num1){
  9. echo 'num小于num1';
  10. }else{
  11. echo 'num大于num1';
  12. }
  13. //2.1使用三元运算符简化分支
  14. echo $num < $num1 ? 'num小于num1':'num大于num1';
  15. //3.多分支
  16. $a = 200;
  17. $b = 200;
  18. if($a==$b){
  19. echo 'a=b';
  20. }elseif ($a>$b){
  21. echo 'a > b';
  22. }elseif ($a<$b){
  23. echo 'a < b';
  24. }
  25. //3.1使用模板语法定义多分支
  26. $a = 1200;
  27. $b = 200;
  28. if ($a == $b):
  29. echo 'a=b';
  30. elseif ($a > $b):
  31. echo 'a > b';
  32. else:
  33. echo 'a < b';
  34. endif;
  35. //3.2使用swich简化多分支
  36. $a = 200;
  37. $b = 1200;
  38. switch (1) {
  39. case $a > $b:
  40. echo 'a > b';
  41. break;
  42. case $a == $b:
  43. echo 'a = b';
  44. break;
  45. default:
  46. echo 'a < b';
  47. }
  48. //3.2.1使用模板语法演示swich
  49. switch (1) :
  50. case $a > $b:
  51. echo 'a > b';
  52. break;
  53. case $a == $b:
  54. echo 'a = b';
  55. break;
  56. default:
  57. echo 'a < b';
  58. endswitch;
  59. //3.2.2 在条件中使用逻辑运算符
  60. $score=95;
  61. switch ($score):
  62. case $score>=60&&$score<=80:
  63. echo '一般';
  64. break;
  65. case $score>80||$score<=100:
  66. echo '优秀';
  67. break;
  68. default:
  69. echo '不及格';
  70. break;
  71. endswitch;

二、循环

  • while 循环,只要 while 表达式的值为 TRUE 就重复执行嵌套中的循环语句。表达式的值在每次开始循环时检查,所以即使这个值在循环语句中改变了,语句也不会停止执行,直到本次循环结束。

示例:

  1. $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  2. $i = 0;
  3. while ($i < count($arr)) {
  4. echo $arr[$i] . '<br>';
  5. $i++;
  6. }
  7. echo '<hr>';
  8. //使用current()和next()
  9. //current()返回数组中的当前单元
  10. //next() - 将数组中的内部指针向前移动一位
  11. while ($item = current($arr)) :
  12. echo $item;
  13. next($arr);
  14. endwhile
  15. echo '---while循环<hr>';
  • do-while 循环,和while 循环区别在于表达式的值是在每次循环结束时检查而不是开始时
  1. //重置指针位置
  2. reset($arr);
  3. //因为第一次循环的时候$item并没有被赋值,所以输出结果会少1位
  4. do{
  5. echo $item;
  6. next($arr);
  7. }while($item = current($arr));
  8. echo '---do while循环<hr>';
  • for循环
    1. for ($i=0;$i<count($arr);$i++){
    2. // break; 提前终止循环,直接跳出
    3. // continue; 终止当前循环,开始下一轮
    4. echo $arr[$i];
    5. }
    6. echo '---for循环<hr>';
  • foreach循环,foreach 仅能够应用于数组和对象
  1. <?php
  2. $data = [
  3. ['id' => 1, 'name' => 'jack', 'age' => 22],
  4. ['id' => 2, 'name' => ' Alexander', 'age' => 32],
  5. ['id' => 3, 'name' => 'Anastasia ', 'age' => 42]
  6. ];
  7. //每次循环中,当前单元的值被赋给 $item 并且数组内部的指针向前移一步
  8. foreach ($data as $item) {
  9. echo $item['id'] . '->' . $item['name'] . '->' . $item['age'] . '<br>';
  10. }

示例:

  1. <?php
  2. $data = [
  3. ['id' => 1, 'name' => 'jack', 'age' => 22],
  4. ['id' => 2, 'name' => ' Alexander', 'age' => 32],
  5. ['id' => 3, 'name' => 'Anastasia ', 'age' => 42]
  6. ]
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>for循环示例</title>
  14. </head>
  15. <body>
  16. <table border="1">
  17. <thead>
  18. <tr>
  19. <th>ID</th>
  20. <th>name</th>
  21. <th>age</th>
  22. </tr>
  23. </thead>
  24. <?php foreach ($data as $item) : ?>
  25. <tr>
  26. <td><?php echo $item['id'] ?></td>
  27. <td><?php echo $item['name'] ?></td>
  28. <td><?php echo $item['age'] ?></td>
  29. </tr>
  30. <?php endforeach; ?>
  31. </table>
  32. </body>
  33. </html>

总结;初步认识PHP分支和循环以及常用逻辑运算符,对于循环嵌套感觉还有点绕

Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:循环是很重要的!一定要吃透,不理解可以多写写,多想想。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post