PHP学习笔记2-流程控制

条件控制:if

 1 <?php
 2 function getLevel($score){
 3     if($score>=90){
 4         return "优秀";
 5     }elseif($score>=80){
 6         return "良好";
 7     }elseif($score>=70){
 8         return "中等";
 9     }elseif($score>=60){
10         return "及格";
11     }else{
12         return "差";
13     }
14 }
15 echo getLevel(90);

多分支控制:switch

 1 <?php
 2 function getLevel($score){
 3     switch(intval($score/10)){ //intval()把变量转换成整数类型
 4         case 10:
 5         case 9:
 6             return "优秀";
 7         case 8:
 8             return "良好";
 9         case 7:
10             return "中等";
11         case 6:
12             return "及格";
13         default:
14             return "差";
15     }
16 }
17 echo getLevel(90);

Break(终止语句执行)

 1 <?php
 2 function getLevel($score){
 3     $result = "差";
 4     switch($score/10) {
 5         case 10:
 6         case 9:
 7             $result = "优秀";
 8             break;
 9         case 8:
10             $result = "良好";
11             break;
12         case 7:
13             $result = "中等";
14             break;
15         case 6:
16             $result = "及格";
17             break;
18         default:
19             return "差";
20     }
21     return $result;
22 }
23 echo getLevel(90);

 

你可能感兴趣的:(学习笔记)