Blogger Information
Blog 16
fans 0
comment 0
visits 11224
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分支机构以及循环遍历数组
evan
Original
978 people have browsed it

单向分支

  1. <?php
  2. // $x = 5; //定义一个整型变量$x,值为5
  3. // $y = 10; //定义一个整型变量$y,值为10
  4. // if ($x < $y) { //$x是小于$y的,所以执行下面语句块
  5. // $t = $x; //先将$x的值放到临时的变量$t中
  6. // $x = $y; //再将变量$y的值赋给变量$x
  7. // $y = $t; //再将临时变量$t中的值赋给变量$y
  8. // } //语句块结束的花括号
  9. // var_dump($x > $y); //两个变量的值已经交换,输出true
  10. // echo $x . " " . $y; //输出 10 5
  11. // 模板化
  12. $x = 5;
  13. $y = 10;
  14. if ($x < $y) :
  15. $t = $x;
  16. $x = $y;
  17. $y = $t;
  18. endif;
  19. var_dump($x > $y);
  20. echo $x . " " . $y; //输出 10 5

双向分支

  1. <?php
  2. // 双向条件分支 ,if ()else{...}
  3. $x =20;
  4. $y =10;
  5. //判断的条件成立,此句会执行
  6. if($x >$y){
  7. echo "坚持就会胜利<br>";
  8. //判断的条件不成立,此句会执行
  9. }else{
  10. echo "不要气馁,再坚持一下就会胜利 <br>";
  11. }

多向分支

  1. <?php
  2. $amuont = 2500;
  3. if ($amuont > 5000 && $amuont < 10000) {
  4. $payment = $amuont * 0.9;
  5. } elseif ($amuont >= 10000 && $amuont < 20000) {
  6. $payment = $amuont * 0.7;
  7. } elseif ($amuont >= 20000 && $amuont < 30000) {
  8. $payment = $amuont * 0.5;
  9. } else {
  10. $payment = $amuont;
  11. }
  12. echo '实际支付:', $payment, '<br>';
  13. //switch简化多分支机构
  14. $week = date("D"); //获取当前的星期值,如Mon、Tue、Wed等
  15. switch( $week ) {
  16. case "Mon": echo "星期一"; break;
  17. case "Tue": echo "星期二"; break;
  18. case "Wed": echo "星期三"; break;
  19. case "Thu": echo "星期四"; break;
  20. case "Fri": echo "星期五"; break;
  21. case "Sat": echo "星期六"; break;
  22. case "Sun": echo "星期日"; break;
  23. }

循环

  1. <?php
  2. // 循环
  3. $cities = ['合肥', '南京', '杭州', '苏州', '上海'];
  4. // 判断型循环:入口判断型 while(条件){...}
  5. // 获取当前数组元素的值
  6. // echo current($cities);
  7. // next($cities);
  8. // echo current($cities);
  9. // next($cities);
  10. // echo current($cities);
  11. // next($cities);
  12. // echo current($cities);
  13. // next($cities);
  14. // echo current($cities);
  15. // 进入循环代码之前进行判断,只有条件为真才执行循环体
  16. // while ($city = current($cities)) {
  17. // echo $city, '<br>';
  18. // // 前移指针
  19. // next($cities);
  20. // }
  21. // reset($cities);
  22. // while ($city = current($cities)) {
  23. // echo $city, '<br>';
  24. // // 前移指针
  25. // next($cities);
  26. // }
  27. // 判断型循环:出口判断型 do{...}while(条件)
  28. // reset($cities);
  29. // do {
  30. // echo $city, '<br>';
  31. // // 循环体内必须要有更新循环条件的语句,否则进入死循环
  32. // next($cities);
  33. // } while ($city = current($cities));
  34. // do while没有对应的模版语法
  35. // 计数型循环:for(循环变量的初始化,循环条件;更新循环条件){...}
  36. // echo count($cities);
  37. // $i:数组元素的索引,默认从0开始
  38. // for ($i = 0; $i < count($cities); $i++) {
  39. // echo $cities[$i], '<br>';
  40. // }
  41. for ($i = 0; $i < count($cities); $i++) {
  42. // 输出拦截
  43. // if ($i > 2) break;
  44. // 有选择输出,跳过一部分
  45. if ($i === 3 || $i === 2) continue;
  46. echo $cities[$i], '<br>';
  47. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!