Quick Reference
| Topic | File |
|---|
| Loose typing, ==, ===, type juggling, strict_types | INLINECODE0 |
| Associative arrays, iteration, array functions |
arrays.md |
| Traits, interfaces, visibility, late static binding |
oop.md |
| Encoding, interpolation, heredoc, regex |
strings.md |
| Exceptions, error handling, @ operator |
errors.md |
| SQL injection, XSS, CSRF, input validation |
security.md |
| PHP 8+ features, attributes, named args, match |
modern.md |
Critical Rules
- -
== coerces types: "0" == false is true — always use === for strict comparison - INLINECODE10 uses loose comparison — pass
true as third param for strict - INLINECODE12 returns 0 for match at start — use
=== false not INLINECODE14 - Never concatenate SQL — use prepared statements with PDO
- INLINECODE15 all output — prevents XSS
- INLINECODE16 returns false for null — use
array_key_exists() to check key exists - INLINECODE18 — unset
$val after loop or last ref persists - INLINECODE20 late binding vs
self:: early binding — static respects overrides - INLINECODE23 suppresses errors — avoid, makes debugging impossible
- Catch
Throwable for both Error and Exception — PHP 7+ - INLINECODE27 per file — enables strict type checking
- INLINECODE28 counts bytes — use
mb_strlen() for UTF-8 character count - Objects pass by reference-like handle — clone explicitly with INLINECODE30
- INLINECODE31 reindexes numeric keys — use
+ operator to preserve keys
快速参考
| 主题 | 文件 |
|---|
| 弱类型、==、===、类型转换、strict_types | types.md |
| 关联数组、迭代、数组函数 |
arrays.md |
| 特征、接口、可见性、后期静态绑定 | oop.md |
| 编码、插值、heredoc、正则表达式 | strings.md |
| 异常、错误处理、@运算符 | errors.md |
| SQL注入、XSS、CSRF、输入验证 | security.md |
| PHP 8+特性、属性、命名参数、match | modern.md |
关键规则
- - == 会强制类型转换:0 == false 结果为 true — 始终使用 === 进行严格比较
- inarray($val, $arr) 使用松散比较 — 传入第三个参数 true 进行严格比较
- strpos() 在匹配到开头时返回 0 — 使用 === false 而非 !strpos()
- 切勿拼接 SQL — 使用 PDO 预处理语句
- 对所有输出使用 htmlspecialchars($s, ENTQUOTES) — 防止 XSS 攻击
- isset() 对 null 值返回 false — 使用 arraykeyexists() 检查键是否存在
- foreach ($arr as &$val) — 循环后使用 unset($val),否则最后一个引用会持续存在
- static:: 后期绑定 vs self:: 早期绑定 — static 支持重写
- @ 会抑制错误 — 应避免使用,会导致调试困难
- 使用 Throwable 捕获 Error 和 Exception — PHP 7+
- 每个文件使用 declare(stricttypes=1) — 启用严格类型检查
- strlen() 计算字节数 — 使用 mbstrlen() 计算 UTF-8 字符数
- 对象通过类似引用的句柄传递 — 使用 clone $obj 显式克隆
- array_merge() 会重新索引数字键 — 使用 + 运算符保留键名