Blogger Information
Blog 8
fans 0
comment 3
visits 8174
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
if和switch分支判断以及for和while循环控制以及模版语法的改写以及运算符“”"&&""||""??”作用及使用场景
程东旭
Original
813 people have browsed it

4月17日作业

1. 将课件中所列的流程控制所有类型, 自举例进行演示并发布到作业中

php代码

  1. <?php
  2. //一.分支举例
  3. //车速
  4. $speed = 200;
  5. //罚款
  6. $punishment = 200;
  7. //1.单分支
  8. if ($speed > 120){
  9. $punishment = $punishment * 1;
  10. }
  11. echo '车速>' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  12. echo '<hr>';
  13. //单分支模版语法
  14. if ($speed > 120):
  15. $punishment = $punishment * 1;
  16. endif;
  17. echo '车速>' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  18. echo '<hr>';
  19. //2.双分支
  20. if ($speed > 120){
  21. $punishment = $punishment * 1;
  22. }else {
  23. $punishment = $punishment * 0;
  24. }
  25. echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  26. echo '<hr>';
  27. //双分支模版语法
  28. if ($speed > 120):
  29. $punishment = $punishment * 1;
  30. else:
  31. $punishment =$punishment * 0;
  32. endif;
  33. echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  34. echo '<hr>';
  35. //3.多分支
  36. if ($speed > 120 && $speed <= 140){
  37. $punishment = $punishment * 1;
  38. }elseif ($speed > 140 && $speed <= 180) {
  39. $punishment = $punishment * 1.2;
  40. }elseif ($speed > 180) {
  41. $punishment =$punishment *1.5;
  42. }else {
  43. $punishment =$punishment * 0;
  44. }
  45. echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  46. echo '<hr>';
  47. //多分支模版语法
  48. if ($speed > 120 && $speed <=140):
  49. $punishment = $punishment * 1;
  50. elseif ($speed >140 && $speed <= 180):
  51. $punishment = $punishment *1.2;
  52. elseif ($speed > 180):
  53. $punishment = $punishment * 1.5;
  54. else:
  55. $punishment = $punishment * 0;
  56. endif;
  57. echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  58. echo '<hr>';
  59. //4.switch 简化多分支
  60. //车速
  61. $speed = 190;
  62. //罚款
  63. $punishment = 200;
  64. switch (true):
  65. case $speed > 120 && $speed <= 140:
  66. $punishment = $punishment * 1;
  67. break;
  68. case $speed > 140 && $speed <= 180;
  69. $punishment = $punishment * 1.2;
  70. break;
  71. case $speed >180;
  72. $punishment = $punishment * 1.5;
  73. break;
  74. default:
  75. die('不罚款');
  76. endswitch;
  77. echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
  78. echo '<hr>';
  79. //二.循环举例
  80. //建立数组
  81. $names = ['马飞', '韩伟', '三民', '张佳林', '小孙'];
  82. //1.while循环
  83. while ($name = current($names)){
  84. echo $name . '<br>';
  85. next($names);
  86. }
  87. //指针复位
  88. reset($names);
  89. echo '<hr>';
  90. //while改为模版语法
  91. while ($name = current($names)):
  92. echo $name. '<br>';
  93. next($names);
  94. endwhile;
  95. echo '<hr>';
  96. //2. 计数型
  97. reset($names);
  98. echo '数组仲有' . count($names);
  99. echo '<hr>';
  100. for ($i = 5; $i > -1; $i--){
  101. echo $names[$i] . '<br>';
  102. }
  103. echo '<hr>';
  104. //模版语法
  105. reset($names);
  106. for ($i = 0; $i < count($names); $i++):
  107. echo $names[$i];
  108. endfor;
  109. echo '<hr>';
  110. $names = ['马飞', '韩伟', null, '三民', '张佳林', '小孙'];
  111. //continue 终止当前循环,提前进入下一轮循环
  112. reset($names);
  113. for ($i= 0; $i < count($names); $i++):
  114. //跳过空数据
  115. if (!$names[$i]) break;
  116. echo $names[$i] . '<br>';
  117. if ($names[$i] === '三民') continue;
  118. endfor;

2. 将运算符中认为不熟悉的自己举例完成并发布

php代码

  1. echo '<hr>';
  2. //太空船运算符
  3. //这里运算的结果如果是-1,说明左边小于右边,如果是1,说明左边大于右边,如果是0,说明两边相等
  4. var_dump(30 <=> 20);
  5. echo '<hr>';
  6. //&&所有的条件必须同时满足返回是1,否则是0,可以理解成"和"
  7. var_dump(3>2 && 5<40 && 8>2);
  8. echo '<hr>';
  9. //|| 条件当中只要有一个值为1,返回值就是true,可以理解成"或"
  10. var_dump(3>20 || 5>8);
  11. echo '<hr>';
  12. $name1 = 'admin';
  13. $name2 = 'chengdongxu';
  14. if (@$name1 && $name2 === 'admin'){
  15. echo '欢迎您';
  16. }else {
  17. echo '未登陆';
  18. }
  19. echo '<hr>';
  20. //简化版双分支
  21. if (@$name1 && $name2 === 'chdongxu') echo '有此人'; else echo '无此人';
  22. echo '<hr>';
  23. echo (@$name1 || $name2 === 'chengdongxu')? '有此人' : '无此人';
  24. echo '<hr>';
  25. //合并运算符
  26. $nam3 = 'mafei';
  27. $name3 = isset($name3) ? $name3 : 'root';
  28. //使用合并运算符
  29. $name3 = $name3 ?? 'cuowu';
  30. echo $name3;
Correcting teacher:天蓬老师天蓬老师

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