變數
命名規則
- 以
$ 開頭,後接字母/底線,再接任意字母/數字/底線:$var_name。
- 區分大小寫:
$var ≠ $Var ≠ $VAR。
- 慣例:
snake_case (函數/變數)、PascalCase (類別)、UPPER_SNAKE (常數)。
動態型別與型別變換
<?php
$foo = "0"; // string "0" (ASCII 48)
$foo++; // string "1" (ASCII 49) - 字串遞增
$foo += 1; // int 2 - 數學運算轉為整數
$foo = $foo + 1.3; // float 3.3 - 加上浮點數轉為浮點數
$foo = 5 + "10 Little Piggies"; // int 15 - 字串開頭數字參與運算
$foo = 5 + "10 Small Pigs"; // int 15
?>
型別檢查函數
| 函數 | 檢查型別 |
| gettype() | 回傳型別字串 |
| is_int() / is_integer() / is_long() | 整數 |
| is_float() / is_double() / is_real() | 浮點數 |
| is_string() | 字串 |
| is_array() | 陣列 |
| is_object() | 物件 |
| is_bool() | 布林 |
| is_null() | null |
| is_numeric() | 數字或數字字串 |
| is_scalar() | 純量 (int/float/string/bool) |
| is_callable() | 可呼叫 |
| is_iterable() | 可迭代 (PHP 7.1+) |
型別強制轉換 (Casting)
<?php
$foo = 10;
$bar = (double) $foo; // float 10.0
$bar = (int) $foo; // int 10
$bar = (string) $foo; // string "10"
$bar = (array) $foo; // array [0 => 10]
$bar = (object) $foo; // object stdClass {scalar: 10}
$bar = (bool) $foo; // bool true
$bar = (unset) $foo; // null (PHP 7.2+)
?>
允許空白:( int ) = (int)。
字串轉數字規則
- 含
.、e、E → 浮點數。
- 否則 → 整數。
- 從開頭解析數字,非數字開頭 → 0。
<?php
1 + "10.5" // float 11.5
1 + "-1.3e3" // float -1299
1 + "bob-1.3e3" // int 1 (非數字開頭)
1 + "bob3" // int 1
1 + "10 Small Pigs" // int 11
?>
常數
define() 定義
<?php
// 語法:bool define(string $name, mixed $value, bool $case_insensitive = false)
define('FOO', 'something');
define('FOO2', 'something else');
define('FOO_BAR', 'something more');
// 非區分大小寫 (不建議,易混淆)
define('GREETING', 'Hello', true);
echo greeting; // Hello
// 非法:define('2FOO', 'x'); // 數字開頭
// 避免 __NAME__ 格式 (保留給魔術常數)
?>
const 關鍵字 (類別/命名空間層級,PHP 5.3+)
<?php
namespace App\Config;
const API_KEY = 'abc123';
const MAX_RETRY = 3;
// 編譯期常數,效能優於 define(),支援 IDE 自動完成
?>
魔術常數
| 常數 | 說明 | 版本 |
| __LINE__ | 檔案當前行號 | 所有 |
| __FILE__ | 完整路徑檔名 (已解析符號連結,PHP 4.0.2+) | 所有 |
| __DIR__ | 檔案所在目錄 (等同 dirname(__FILE__)) | 5.3.0+ |
| __FUNCTION__ | 函數名稱 (區分大小寫,PHP 5+) | 4.3.0+ |
| __CLASS__ | 類別名稱 (區分大小寫,PHP 5+) | 4.3.0+ |
| __TRAIT__ | Trait 名稱 | 5.4.0+ |
| __METHOD__ | 類別方法名稱 | 5.0.0+ |
| __NAMESPACE__ | 當前命名空間 (大小寫敏感) | 5.3.0+ |
超全域變數
| 變數 | 來源/用途 | 舊式對應 (棄用) |
| $GLOBALS | 所有全域變數參考 (鍵=變數名) | - |
| $_SERVER | 伺服器/執行環境資訊 | $HTTP_SERVER_VARS |
| $_GET | URL 查詢字串參數 | $HTTP_GET_VARS |
| $_POST | POST 表單資料 | $HTTP_POST_VARS |
| $_COOKIE | HTTP Cookies | $HTTP_COOKIE_VARS |
| $_FILES | 上傳檔案資訊 | $HTTP_POST_FILES |
| $_ENV | 環境變數 | $HTTP_ENV_VARS |
| $_REQUEST | GET/POST/COOKIE 合併 (順序看 variables_order) | - |
| $_SESSION | Session 變數 | $HTTP_SESSION_VARS |
運算子優先序 (高→低)
1. clone, new
2. ** (右結合)
3. ++, --, ~, (int), (float), (string), (array), (object), (bool), @
4. instanceof
5. ! (右結合)
6. *, /, %
7. +, -, .
8. <<, >>
9. <, <=, >, >=
10. ==, !=, ===, !==, <>, <=>
11. &
12. ^
13. |
14. &&
15. ||
16. ? : (右結合)
17. =, +=, -=, *=, **=, /=, .=, %=, &=, |=, ^=, <<=, >>= (右結合)
18. and
19. xor
20. or
21. , (逗號運算子)
常用運算子速查
| 分類 | 運算子 | 範例 | 結果 |
| 算術 | + | $x=2; $x+2 | 4 |
| - | $x=2; 5-$x | 3 |
| * | $x=4; $x*5 | 20 |
| / | 15/5 | 3 |
| % | 5%2 | 1 |
| ** | 2**3 | 8 |
| 字串 | . | "Hello" . " World" | "Hello World" |
| .= | $a="Hi"; $a.="!" | "Hi!" |
| 複合指派 | += | $x+=2 | $x=$x+2 |
| -= | $x-=2 | $x=$x-2 |
| *= | $x*=2 | $x=$x*2 |
| /= | $x/=2 | $x=$x/2 |
| %= | $x%=2 | $x=$x%2 |
| .= | $x.="!" | $x=$x."!" |
| 遞增/遞減 | ++$x | 前置:先增後用 | |
| $x++ | 後置:先用後增 | |
| --$x / $x-- | 同理 | |
三元運算子與 Null 合併運算子
<?php
// 三元運算子
$a = 1;
echo $a ? 'true' : 'false'; // true
// 巢狀需加括號
echo ($x >= 0) ? $x : -$x; // 絕對值
// Elvis 運算子 (PHP 5.3+)
$name = $user['name'] ?? 'Guest'; // isset($user['name']) ? $user['name'] : 'Guest'
// Null 合併指派 (PHP 7.4+)
$data['count'] ??= 0; // 等同 $data['count'] = $data['count'] ?? 0;
?>