[PHP_Day 3]_基本語法part3_單行條件判斷(三元運算子)_switch case


單行條件判斷(三元運算子)_switch case


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

 $res = true;
 // Before
 if($res)
  echo "真<br/>";
 else
  echo "假<br/>";
 // After
 $str = ($res == true) ? "真<br/>" : "假<br/>";
 echo $str;

 $result = 8;
 // Before
 if($result == 1)
  echo "1<br/>";
 else if($result ==2)
  echo "2<br/>";
 else if($result ==3)
  echo "3<br/>";
 else
  echo "else<br/>";
 // After
 
 switch($result){
  case 1:
   echo "case.1<br/>";
   echo "1<br/>";
   break;
  case 2:
   echo "case.2<br/>";
   break;
  case 3:
   echo "case.3<br/>";
   break;
  default:
   echo "default<br/>";
   break;
 }
 
 $strCase = "紅豆";
 switch($strCase){
  case "紅豆":
   echo "case.紅豆<br/>";
   break;
  case 2:
   echo "case.2<br/>";
   break;
  case "綠豆":
   echo "case.綠豆<br/>";
   break;
  default:
   echo "default<br/>";
   break;
 }
 
 
?>




留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header