PHP 速查表
一份可搜尋、可列印的 PHP 8.3+ 參考——語法、字串、陣列、函式、類別、列舉、屬性和現代特性。免費。
Basics & types
10declare(strict_types=1);
Enforce strict scalar type checks
$x = 1;
Variables start with a dollar sign
const MAX = 100;
Compile-time constant
gettype($x)
Get the type of a value
(int) $x
Cast to an integer
is_int($x)
Type-check helper (is_string, ...)
int|string $x
Union type declaration
?string $x
Nullable type (string or null)
$a ?? 'default'
Null coalescing operator
$a ??= 'default'
Assign only if null
Strings
11"Hello $name"
Double quotes interpolate variables
'literal $x'
Single quotes are literal
$a . $b
Concatenate strings
strlen($s)
Byte length of a string
str_contains($s, 'a')
Check for a substring
str_starts_with($s, 'a')
Check the prefix
substr($s, 0, 3)
Extract part of a string
str_replace('a', 'b', $s)
Replace all occurrences
explode(',', $s)
Split into an array
sprintf('%05.2f', $n)
Formatted string output
trim($s)
Strip surrounding whitespace
Arrays
11$a = [1, 2, 3];
Indexed array literal
$a = ['k' => 'v'];
Associative array
array_map(fn($x) => $x * 2, $a)
Transform each element
array_filter($a, fn($x) => $x > 0)
Keep matching elements
array_reduce($a, $fn, 0)
Reduce to a single value
in_array($v, $a, true)
Strict value check
array_keys($a)
Get all keys
array_merge($a, $b)
Merge arrays
[...$a, ...$b]
Spread/merge arrays
count($a)
Number of elements
[$x, $y] = $a;
Array destructuring
Functions
9function f(int $a): int {}
Typed parameters and return
function f(int $a = 1) {}
Default parameter value
function f(int ...$nums) {}
Variadic parameters
f(...$args)
Spread arguments
f(name: 'Sam', age: 30)
Named arguments
$fn = fn($x) => $x + 1;
Arrow function (auto-captures scope)
function () use ($x) {}
Closure capturing a variable
$fn = strlen(...);
First-class callable syntax
function f(): never {}
Never returns (throws or exits)
Control flow
9if ($a) {} elseif ($b) {} else {}
Conditional branches
$a ? $b : $c
Ternary expression
$a ?: $b
Short ternary (falsy fallback)
match ($x) { 1, 2 => 'a', default => 'b' }
Strict, expression-based match
switch ($x) { case 1: break; }
Loose multi-way branch
foreach ($a as $k => $v) {}
Iterate keys and values
for ($i = 0; $i < 10; $i++) {}
Counted loop
while ($cond) {}
Loop while a condition holds
break / continue
Exit or skip a loop iteration
Classes & OOP
10class A extends B implements C {}
Inheritance and interfaces
public function __construct(private int $id) {}
Constructor property promotion
public readonly string $name;
Immutable after initialization
public function f(): static {}
Return the late-bound type
static::create()
Late static binding
$obj?->method()
Null-safe method call
abstract class A {}
Cannot be instantiated directly
trait T {} use T;
Reusable horizontal code
$obj instanceof A
Type check at runtime
A::class
Fully-qualified class name string
Enums
9enum Status { case Active; case Draft; }
Pure enumeration
enum Status: string { case A = 'a'; }
Backed enum (string/int)
Status::Active
Reference an enum case
Status::from('a')
Build from a backing value
Status::tryFrom('x')
Returns null if invalid
Status::cases()
Array of all cases
$status->value
Backing value of a case
$status->name
Name of the case
enum E { public function label() {} }
Enums can have methods
Error handling
9try {} catch (Throwable $e) {}
Catch errors and exceptions
catch (TypeError | ValueError $e)
Catch multiple types
catch (Exception)
Non-capturing catch (no variable)
finally {}
Always runs after try/catch
throw new RuntimeException('x')
Throw an exception
throw $e;
Re-throw the caught exception
$x = $v ?? throw new Error();
Throw as an expression
$e->getMessage()
Read the error message
$e->getPrevious()
Get the chained exception
Attributes & modern
9#[Attribute] class Route {}
Declare a custom attribute
#[Route('/home')]
Apply an attribute to a target
new ReflectionClass($x)
Inspect a class via reflection
$ref->getAttributes()
Read declared attributes
json_encode($data)
Serialize to JSON
json_decode($s, true)
Decode JSON to an array
array_is_list($a)
True if keys are 0..n in order
str_word_count($s)
Count words in a string
$obj::class
Class name from an instance
沒有條目符合「:q」。
關於 PHP 速查表
這份 PHP 速查表將現代 PHP 濃縮成一個可搜尋的頁面:基礎與型別、字串、陣列、函式、控制流程、類別與物件導向、列舉、錯誤處理,以及屬性標記與其他現代特性。每一行都是可執行的程式碼片段,搭配淺白的說明。
它以 PHP 8.3 以上版本為目標,因此您會找到定義當代 PHP 的各項特性——列舉、唯讀屬性、建構子屬性提升、具名引數、match 運算式、箭頭函式、nullsafe 呼叫與屬性標記——同時也涵蓋您經常會用到的日常字串與陣列函式。
這個頁面免費且完全在用戶端運作:輸入文字時搜尋會即時篩選各行內容,固定式區段選單可帶您跳轉,一鍵即可複製任何程式碼片段,列印按鈕能將其轉成紙本的 PHP 參考。
如何使用 PHP 速查表
- 瀏覽目錄——從「基礎與型別」到「列舉」、「錯誤處理」與「屬性標記與現代特性」。
- 在搜尋框中輸入函式名稱或關鍵字,即時篩選整份速查表。
- 透過固定式側邊導覽跳到「類別與物件導向」等區段。
- 點擊任何程式碼片段或其複製圖示,將 PHP 程式碼複製到剪貼簿。
- 若想要紙本版的完整參考,可點擊「列印」。
常見問題
它是為 PHP 8.3 及更新版本撰寫的。列舉、屬性標記與現代特性等區段,除了經典語法外,還涵蓋唯讀屬性、建構子提升、match 運算式、具名引數與 nullsafe 呼叫。
九個區段:基礎與型別、字串、陣列、函式、控制流程、類別與物件導向、列舉、錯誤處理,以及屬性標記加上現代特性——都是您在實際專案中每天會用到的 PHP 部分。
使用頁面頂端的搜尋框。它會即時篩選所有區段中的每一行,並顯示符合數量,因此輸入像 array_map 這樣的關鍵字,就能立即縮小到相關的程式碼片段。
可以。點擊程式碼本身或滑鼠懸停時出現的複製圖示,程式碼片段就會進入剪貼簿,並以短暫的「已複製!」訊息確認。
是的,完全免費。它在您的瀏覽器中渲染,不需要帳號,也可以隨意列印。
熱門搜尋
php cheat sheet
php syntax reference
php array functions
php string functions
php enums example
php 8 attributes
php oop cheat sheet
php try catch example
需要協助?
使用此工具時遇到問題?請告訴我們的團隊。