Blogger Information
Blog 28
fans 0
comment 1
visits 21447
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
深刻理解分支结构,特别是多分支与switch,并实例演示
南宫
Original
856 people have browsed it

1. 单分支

  1. $age = 19;
  2. if ($age > 18){
  3. echo "已满十八周岁";
  4. }
  5. //当代码块中只有一行代码的时候,可以省略大括号
  6. if($age > 18) echo '已成年!!';
  7. 模板语法写法:
  8. if($age>18):
  9. echo '已成年!!';
  10. endif;
  11. //模板语法: 就是干掉流程控制中的大括号,将大括号用冒号代替,将结束大括号用特殊关键字代替

2. 双分支

  1. $age = 13;
  2. if($age > 18){
  3. echo '已满十八周岁';
  4. }else{
  5. echo '未满十八周岁';
  6. }
  7. //模板语法:
  8. $age = 13;
  9. if($age > 18):
  10. echo '已成年!';
  11. else:
  12. echo '未成年!!!';
  13. endif;

3.多分支

if 处理多分支
  1. $number = 74;
  2. if($number > 90){
  3. echo '优秀';
  4. }elseif ($number > 80 && $number <= 90){
  5. echo '良好';
  6. }elseif ($number >= 60 && $number <= 80){
  7. echo '及格';
  8. }else{
  9. echo '不及格';
  10. }
  11. //模板语法:
  12. $number = 74;
  13. if($number > 90):
  14. echo '优秀';
  15. elseif ($number > 80 && $number <= 90):
  16. echo '良好';
  17. elseif ($number >= 60 && $number <= 80):
  18. echo '及格';
  19. else:
  20. echo '不及格';
  21. endif;
switch 处理多分支
  1. //switch(表达式){
  2. // case 条件1:
  3. // 分支语句
  4. // break;
  5. // case...
  6. // default:
  7. // ...
  8. //}
  9. $sex = '男';
  10. switch($sex){
  11. case '男':
  12. echo '我是小哥哥!';
  13. break;
  14. case '女':
  15. echo '我是小姐姐!';
  16. default:
  17. echo '我既不是是小姐姐,也不是小哥哥!';
  18. break;
  19. }
  20. //模板语法
  21. $sex = '男';
  22. switch($sex):
  23. case '男':
  24. echo '我是小哥哥';
  25. break;
  26. case '女':
  27. echo '我是小姐姐';
  28. break;
  29. default:
  30. echo '我既不是小姐姐,也不是小哥哥!';
  31. break;
  32. endswitch;
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