[PHP_Day 2]_基本語法part2_字串、數字型態、布林_算數運算_比較運算_邏輯運算
字串、數字型態、數值相關內建函數操作
型態打印函數:var_dump (常用!!!)
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 59 60 61 62 63 64 65 66 67 68 69 70 | <?php //整數int echo "整數int<br/>"; $intVal = 10; var_dump($intVal); echo "<br/>"; echo $intVal;//背後有進行轉型為string,因為echo只接受string。 echo "<br/>"; echo "負整數int<br/>"; $intVal2 = -88; var_dump($intVal2); echo "<br/>"; echo $intVal2; echo "<br/>"; //16進制表示 echo "整數16進制表示<br/>"; $intHexVal = 0xff; var_dump($intHexVal); echo "<br/>"; echo $intHexVal; echo "<br/>"; //float echo "浮點數float<br/>"; $floatVal = 9.567; var_dump($floatVal); echo "<br/>"; echo $floatVal; echo "<br/>"; echo "浮點數round<br/>"; $floatVal2Digit = round(9.567,2); var_dump($floatVal2Digit); echo "<br/>"; echo $floatVal2Digit; echo "<br/>"; echo "浮點數ceil<br/>"; $floatValCeil = ceil(9.567); var_dump($floatValCeil); echo "<br/>"; echo $floatValCeil; echo "<br/>"; echo "浮點數floor<br/>"; $floatValFloor = floor(9.567); var_dump($floatValFloor); echo "<br/>"; echo $floatValFloor; echo "<br/>"; echo "數值間的CAST運算<br/>"; $var1 = intVal(9.567); var_dump($var1); echo "<br/>"; echo $var1; echo "<br/>"; $var2 = floatVal(9); var_dump($var2); echo "<br/>"; echo $var2; echo "<br/>"; echo "字串跟數字組合輸出<br/>"; echo "數字(整數)1: " . 1 . " 結束<br/>"; echo "數字(浮點數)1.5: " . 1.5 . " 結束<br/>"; ?> |
算數運算
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 | <?php //加減 $num = 15.5; $res = $num - 5 + 1.2; echo $res; echo"<br/>"; //乘法 $num1 = 6.2; $res1 = $num1 * 2; echo $res1; echo"<br/>"; //除法(取整)->除不進的會除到底回傳浮點 $num2 = 12; $res2 = $num2 / 5; echo $res2; echo"<br/>"; //除法(取餘)->仍會回傳對應結果 $num3 = 12; $res3 = $num3 % 5; echo $res3; echo"<br/>"; $result = 3 + 5 * 2; echo $result;//13 echo"<br/>"; $result2 = (3 + 5) * 2; echo $result2;//16 ?> |
算術運算實際常用到的應用情境(時間換算)
幾時幾分幾秒換算
1 2 3 4 5 6 7 8 9 | <?php $traffic_secs = 7268; $hours = intval($traffic_secs / (60*60));//只取整數部分 echo "Hour:$hours<br/>"; $minutes = intval($traffic_secs / 60) % 60; echo "Minutes:$minutes<br/>"; $seconds = $traffic_secs % 60; echo "Seconds:$seconds<br/>"; ?> |
PHP當中比較怪異的部分 字串竟可做算術運算(不建議這麼做)
1 2 3 4 5 | <?php echo "1" + "2"; echo "<br/>"; var_dump("1" + "2"); ?> |
布林值 (True/False)
--->跟整數是有相關的
1 2 3 4 5 6 7 8 9 10 | <?php $var1 = true;//True ,TRUE,TrUe , TRUE var_dump($var1);//當true時會先轉1在轉string做輸出 echo $var1; echo "<br/>"; $var2 = false;//當false時捨麼都不輸出 echo $var2; echo "<br/>"; ?> |
比較運算範本1
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 | <?php $var1 = true;//True ,TRUE,TrUe , TRUE var_dump($var1);//當true時會先轉1在轉string做輸出 echo "<br/>"; echo $var1; echo "<br/>"; $var2 = false;//當false時捨麼都不輸出 var_dump($var2); echo "<br/>"; echo $var2; echo "<br/>"; echo "====if true print====<br/>"; if(true){ echo "Is True<br/>"; } echo "====if false do nothing====<br/>"; if(false){ echo "Is Not True<br/>"; } echo "====if else put boolean====<br/>"; if(false){ echo "Is Not True<br/>"; }else{ echo "Is True<br/>"; } echo "====if else put string empty or not empty====<br/>"; if(""){ echo "Is Not True<br/>"; }else{ echo "Is True<br/>"; } ?> |
假如是非空字串也會視為true
假如是非0數字也會視為true (正負整數、正負浮點數一律視為true)
若是捨麼都沒Assign 或為null則視為false
比較運算範本2
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 | <?php //純數值大小比較 $n1 = 10; if($n1 > 9){ echo "10比9大<br/>"; } if($n1 == 10){ echo "10等於10<br/>"; } if($n1 < 11){ echo "10小於11<br/>"; } //數值跟字串之間比較 $num = 7; $str = "7"; //雙等號比較不考慮資料型態(數值一律都會先轉為string再比對) if($num == $str){ echo "True...<br/>"; }else{ echo "False...<br/>"; } //連型態一起比對 if($num === $str){ echo "True...<br/>"; }else{ echo "False...<br/>"; } //不等於兩種寫法(不考慮資料型態) if($num != 8){ echo "Not equal<br/>"; } if($num <> 8){ echo "Not equal<br/>"; } //不等於寫法(連型態) if($num !== $str){ echo "Not equal<br/>"; } //反相運算 echo true; echo "<br/>"; echo !true; ?> |
邏輯運算(複合條件)
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 | <?php //沒用邏輯運算時情況 $price = 145; $money = 150; if($money > 100){ if($price < 150){ echo "買了<br/>"; } } //使用邏輯運算 if( ($money > 100) && ($price < 150) ){ echo "買了<br/>"; } echo "And運算寫法<br/>"; echo (true and true); echo "<br/>"; echo (true && true); echo "<br/>"; echo (true && false); echo "<br/>"; echo "Or運算寫法<br/>"; echo (true or true); echo "<br/>"; echo (true || true); echo "<br/>"; echo (false || false); echo "<br/>"; echo (true || false); echo "<br/>"; echo "Xor運算寫法<br/>"; echo (true xor true); echo "<br/>"; echo (false xor false); echo "<br/>"; echo (true xor false); echo "<br/>"; ?> |
留言
張貼留言