PHP資料類別 – Let’s Go!
資料類別
資料類別 | ⼤⼩ | 數值範圍 | 例⼦ |
int | 4 bytes | 整數: (32 bit) -2,147,483,648 ⾄ 2,147,483,647 (64bit) -9223372036854775808 ⾄ 9223372036854775807 | <?php $value = 1231; echo $value; ?> |
float | 1.7976931348623E+308 (平台決定), 最大14位精度 | <?php $value = 7.456; echo $value; ?> | |
String | <?php $value = “Hello World”; echo $value; ?> | ||
bool | 對錯: 只有 true 及 false 兩個值 | <?php $x = true; echo $value; ?> |
轉換值
資料類別 | Convert | 例⼦ |
int | $str_result = (string)$value; $float_result = (float)$value; $bool_result = (bool)$value; | <?php $value = 7789; $int_result = (string)$value; echo $int_result; ?> <?php $value = 7789; $float_result = (float) $value; echo $float_result; ?> <?php $value = 1; $bool_result = (bool)$value; echo $bool_result; ?> |
float | $str_result = (string)$value; $int_result = (int)$value; $bool_result = (bool)$value; | <?php $value = 776.89; $int_result = (string) $value; echo $int_result; ?> <? php $value = 776.89; $int_result = (int)$value; echo $int_result; ?> <?php $value = 0; $int_result = (bool)$value; echo $int_result; ?> |
bool | $int_result = (string)$value; $int_result = (int)$value; | <?php $value = true; $int_result = (string) $value; echo $int_result; ?> <?php $value = true; $int_result = (int)$value; echo $int_result; ?> |
string | $str_result = (int)$x; $float_result = (float)$value; $bool_result = (bool)$value; | <?php $x = “22334”; $int_result = (int)$x; echo $int_result; ?> <?php $value = “565456.8”; $float_result = (float) $value; echo $float_result; ?> <?php $value = “1”; $bool_result = (bool)$value; echo $bool_result ; ?> |
const Keyword 常量字段和局部變量不是變量,不能修改。 e.g. <?php const Speed = 14.5; ?> | static Keyword <?php class test { public static $x = 100; } print test::$x . “\n”; // OK ?> |
Global, Local Scope | |
<?php $engineOil = 5; // global scope function Run($speed) { $expense = $speed * $GLOBALS[‘engineOil’]; } Run(4); ?> | Global scope variable –engineOil 能在Class 內調用, run方法內外調用都能 Local scope variable –expense, speed 不能在run方法外調用 |