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 浓缩到一个可搜索的页面中:基础与类型、字符串、数组、函数、控制流、类与面向对象、枚举、错误处理,以及特性(attributes)和其他现代功能。每一行都是可运行的代码片段,配有通俗易懂的说明。
它面向 PHP 8.3+,所以你会在其中找到定义当前 PHP 的各项特性——枚举、只读属性、构造函数属性提升、命名参数、match 表达式、箭头函数、空安全调用和特性(attributes)——以及你经常用到的日常字符串和数组函数。
该页面免费且完全在客户端运行:输入时搜索会实时过滤各行,固定的章节菜单可带你跳转到速查表各处,一键即可复制任意代码片段,打印按钮可将其变成一份纸质 PHP 参考资料。
如何使用 PHP 速查表
- 浏览目录——从"基础与类型"到"枚举"、"错误处理"和"特性与现代功能"。
- 在搜索框中输入函数名或关键词,即时过滤整份速查表。
- 通过固定的侧边栏导航跳转到某个章节,例如"类与面向对象"。
- 点击任意代码片段或其复制图标,将 PHP 代码复制到剪贴板。
- 如果想要整份参考资料的纸质版,点击"打印"。
常见问题
它是为 PHP 8.3 及更新版本编写的。除经典语法外,枚举、特性和现代功能相关章节还涵盖了只读属性、构造函数提升、match 表达式、命名参数和空安全调用。
九个章节:基础与类型、字符串、数组、函数、控制流、类与面向对象、枚举、错误处理,以及特性和现代功能——这些都是你在实际项目中每天都会用到的 PHP 部分。
使用顶部的搜索框。它会在所有章节中实时过滤每一行并显示匹配数量,因此输入类似 array_map 的内容会立即将速查表缩小到相关的代码片段。
可以。点击代码本身或悬停时出现的复制图标,代码片段就会进入剪贴板,并有简短的"已复制!"提示闪现确认。
是的,完全免费。它在你的浏览器中渲染,无需账号,可以随意打印。
热门搜索
php 速查表
php 语法参考
php 数组函数
php 字符串函数
php 枚举示例
php 8 属性
php 面向对象速查表
php try catch 示例
需要帮助?
使用此工具时遇到问题?请告诉我们的团队。