PHP 是弱型別語言,變數型別由賦予的值自動決定。理解四大標量型別(整數、浮點數、布林、字串)的特性、字面值語法、範圍限制與判斷函數,是 PHP 入門的核心基礎。
| 分類 | 型別 | 關鍵字 | 說明 |
|---|---|---|---|
| 標量 | 整數 | int / integer | 整數值,平台相關範圍 |
| 浮點數 | float / double / real | 小數、科學記號,IEEE 754 雙精度 | |
| 布林值 | bool / boolean | true / false | |
| 字串 | string | 位元組序列,無編碼資訊 | |
| 複合 | 陣列 | array | 有序映射,支援整數/字串鍵 |
| 物件 | object | 類別實例 | |
| 特殊 | 資源 | resource | 外部資源控制代碼(檔案、資料庫連線等) |
| Null | null | 無值、未初始化 |
$dec = 1234; // 十進制
$oct = 0123; // 八進制(前綴 0)= 83
$oct = 0o123; // PHP 8.1+ 明確八進制前綴
$hex = 0x1A; // 十六進制(前綴 0x)= 26
$bin = 0b1010; // 二進制(前綴 0b,PHP 5.4+)= 10
$neg = -123; // 負數
PHP_INT_MIN = -9223372036854775808,PHP_INT_MAX = 9223372036854775807PHP_INT_SIZE:位元組數(4 或 8)
var_dump(PHP_INT_MAX); // int(9223372036854775807)
var_dump(PHP_INT_MAX + 1); // float(9.2233720368548E+18)
var_dump(2 ** 63); // float(9.2233720368548E+18)
⚠️ 無警告、無例外,靜默轉型,極易產生邏輯 Bug。
/ 永遠回傳 float(即使整除)intdiv($a, $b) (PHP 7.0+)% 回傳整數& | ^ ~ << >> 僅適用整數
$f1 = 1.234;
$f2 = 1.2e3; // 1200 (科學記號)
$f3 = 7E-10; // 0.0000000007
$f4 = .5; // 0.5(可省略前導 0)
$f5 = 1.; // 1.0(可省略小數後數字)
$a = 0.1 + 0.7; // 0.7999999999999999
floor($a * 10); // 7 (非預期 8)
intval(76.60 * 100); // 7659 (非 7660)!
// 原因:0.1、0.7 二進制無法精確表示(如 1/3 = 0.333...)
// 64 位雙精度約 15-17 位十進制有效數字
// 容許誤差比較
function floatEquals(float $a, float $b, float $eps = 1e-10): bool {
return abs($a - $b) < $eps;
}
floatEquals(0.1 + 0.2, 0.3); // true
// 高精度計算用 BCMath
bcscale(10);
bccomp(bcadd('0.1', '0.2'), '0.3') === 0; // true
// 金額計算:轉「分」整數運算
$cents = (int)round(19.99 * 100); // 1999 分
$yes = true; // 或 TRUE、True
$no = false; // 或 FALSE、False
| 型別 | 值 |
|---|---|
| Boolean | false |
| Integer | 0 |
| Float | 0.0 |
| String | "" (空字串)、"0" |
| Array | [] (空陣列) |
| Object | 無屬性物件(PHP 4),PHP 5+ 物件永遠為 true |
| Null | null |
| Resource | 無(資源永遠為 true) |
"0" == false; // true ⚠️ 字串 "0" 視為 false
"0.0" == false; // false(字串非空)
"false" == false; // false(非空字串)
[] == false; // true
[0] == false; // false(非空陣列)
gettype(123); // "integer"
gettype(3.14); // "double" (非 "float",歷史原因)
gettype(true); // "boolean"
gettype("hello"); // "string"
gettype([]); // "array"
gettype(new StdClass); // "object"
gettype(null); // "NULL"
gettype(fopen('php://memory','r')); // "resource"
⚠️ 不建議用於邏輯判斷:字串比較慢、回傳值可能變更。
is_int(123); // true
is_float(3.14); // true
is_bool(true); // true
is_string("hi"); // true
is_array([]); // true
is_object(new StdClass); // true
is_null(null); // true
is_numeric("123"); // true(含數值字串)
is_scalar(123); // true(int/float/string/bool)
is_callable('strlen'); // true
is_iterable([1,2]); // true (PHP 7.1+)
is_countable([1,2]); // true (PHP 7.3+)
get_debug_type(123); // "int"
get_debug_type(3.14); // "float"
get_debug_type(new Foo()); // "Foo" (類別名)
get_debug_type(fn() => 1); // "Closure"
比 gettype() 更精確、符合型別宣告語法,除錯/日誌/例外訊息首選。
is_int(), is_string(), is_array() 等is_numeric()is_scalar()is_iterable(), is_countable()get_debug_type() (PHP 8.0+) 或 gettype()declare(strict_types=1) 強制函數參數/回傳型別intdiv() 整除is_*,除錯用 get_debug_type(),避開 gettype()declare(strict_types=1) + 型別宣告 + 靜態分析(PHPStan/Psalm)