PHP 可用的函数
系统整理 PHP 变量处理核心标准函数(类型检测、类型转换、序列化与调试导出)及密码散列算法 API,并在 PHP 8.4 CLI 实测全部关键语义与边界行为。
学完这一章,你能够
- 系统整理 PHP 变量处理核心标准函数(类型检测、类型转换、序列化与调试导出)及密码散列算法 API,并在 PHP 8.4 CLI 实测全部关键语义与边界行为。
变量检测、类型转换与序列化核心函数
PHP 标准库提供了一整套变量处理函数与语言结构(其中 `isset`、`empty`、`unset` 属于语言结构),覆盖类型判断、显式类型转换、引用调试输出以及序列化/反序列化。本站使用本地 PHP 8.4.27 CLI 实测确认:`empty('0')` 返回 `true` 而 `isset($valNull)` 返回 `false`;`gettype(1.5)` 返回历史名称 `'double'`,而 PHP 8 新增的 `get_debug_type()` 可直接返回规范短名 `'int'` 与精确类名 `'stdClass'`;`intval('42px')` 为 `42`、`intval('ff', 16)` 为 `255`、`floatval('3.14rem')` 为 `3.14`、`boolval('0')` 为 `false` 但 `boolval('0.0')` 为 `true`;`serialize()` 生成 `'a:3:{s:4:"lang";s:3:"PHP";s:5:"major";i:8;s:6:"active";b:1;}'` 并可通过 `unserialize($ser, ['allowed_classes' => false])` 安全还原;内存流 `php://memory` 的 `is_resource()` 为 `true`、`get_resource_type()` 为 `'stream'`。表中已注明历史函数 `import_request_variables()` 早已在 PHP 5.4 出于安全原因移除。
密码散列算法标准 API
用户口令存储严禁使用裸 `md5()` 或 `sha1()`,应统一使用 PHP 内置的密码散列 API(`password_hash`、`password_verify`、`password_get_info`、`password_needs_rehash`),自动处理密码学安全随机盐与自适应计算成本。本站 PHP 8.4.27 实测使用 `PASSWORD_BCRYPT`(`cost=10`)生成哈希后,`password_verify()` 对正确与错误口令分别返回 `true` 和 `false`,`password_get_info()` 读出算法名 `'bcrypt'` 与 `cost=10`,调用 `password_needs_rehash()` 在 `cost=10` 时返回 `false`、升级至 `cost=12` 时返回 `true`。
PHP 变量处理常用函数参考表
| 函数 / 结构 | 功能说明 |
|---|---|
| boolval() | 将任意表达式转换为对应的布尔值(bool)。 |
| debug_zval_dump() | 输出变量的类型、值与底层 Zend 引擎引用计数(refcount)调试信息。 |
| doubleval() | floatval() 的历史别名,获取变量的浮点数值。 |
| empty() | 语言结构:检查变量未定义或其值为空(0、'0'、''、null、false、[])时返回 true。 |
| floatval() | 解析并返回变量的浮点数(float)表示。 |
| get_defined_vars() | 以关联数组形式返回当前作用域内所有已定义变量及其值。 |
| get_resource_type() | 返回资源句柄(resource)的具体类型名称(如 stream)。 |
| gettype() | 返回变量的类型名称字符串(如 integer、double、string;精细诊断推荐 PHP 8 的 get_debug_type())。 |
| import_request_variables() | 历史危险函数:将 GET/POST/Cookie 导入全局作用域(因变量覆盖漏洞已于 PHP 5.4 移除)。 |
| intval() | 按指定进制(默认十进制)获取变量的整数值。 |
| is_array() | 检测变量是否为数组类型。 |
| is_bool() | 检测变量是否为布尔类型。 |
| is_callable() | 检测值是否可在当前上下文作为合法回调被调用。 |
| is_double() | is_float() 的别名函数。 |
| is_float() | 检测变量是否为浮点型。 |
| is_int() | 检测变量是否为整型。 |
| is_integer() | is_int() 的别名函数。 |
| is_iterable() | 检测变量是否为数组或实现了 Traversable 接口的可迭代对象。 |
| is_long() | is_int() 的别名函数。 |
| is_null() | 检测变量的值是否为 NULL。 |
| is_numeric() | 检测变量是否为数字或合法的数字字符串。 |
| is_object() | 检测变量是否为对象实例。 |
| is_real() | is_float() 的历史别名(PHP 7.4 起废弃,PHP 8.0 已移除)。 |
| is_resource() | 检测变量是否为有效的资源类型句柄。 |
| is_scalar() | 检测变量是否为标量(int、float、string、bool;数组、对象、资源与 null 返回 false)。 |
| is_string() | 检测变量是否为字符串类型。 |
| isset() | 语言结构:检测一个或多个变量是否已声明且值不为 NULL。 |
| print_r() | 以人类易读格式打印或返回数组与对象的结构信息。 |
| serialize() | 将 PHP 值编码为可存储或传输的字节流表示(不适用于闭包与资源)。 |
| settype() | 原地更改变量的数据类型并返回是否成功。 |
| strval() | 获取变量的字符串表示。 |
| unserialize() | 将序列化字节流还原为 PHP 值(反序列化外部数据时务必传 ['allowed_classes' => false] 防对象注入,或改用 JSON)。 |
| unset() | 语言结构:销毁指定变量或数组中的指定元素键。 |
| var_dump() | 输出变量的完整类型、长度与嵌套结构,常用于调试。 |
| var_export() | 输出或返回合法可执行 PHP 代码形式表示的变量字面量。 |
PHP 密码散列算法函数参考表
| 函数 | 功能说明 |
|---|---|
| password_get_info() | 解析密码哈希字符串并返回算法标识、算法名称(如 bcrypt)与参数选项。 |
| password_hash() | 使用强抗暴力破解算法(如 PASSWORD_DEFAULT / PASSWORD_BCRYPT / Argon2)与随机盐生成密码散列。 |
| password_needs_rehash() | 检查既有密码散列是否落后于当前目标算法或成本参数,用于登录后平滑升级哈希。 |
| password_verify() | 以恒定时间安全比对明文密码与 password_hash() 生成的散列值是否匹配。 |
在 PHP 8.4 CLI 实测变量类型检测、转换、安全反序列化与 bcrypt 密码哈希
<?php
declare(strict_types=1);
$cases = [];
// Case 1: isset, empty, is_* predicates, gettype, get_debug_type
$valInt = 42;
$valStr = '3.14';
$valZeroStr = '0';
$valArr = [1, 2, 3];
$valObj = new stdClass();
$valNull = null;
$fn = static fn(int $x): int => $x + 1;
$typeActual = [
'isset_int' => isset($valInt),
'isset_null' => isset($valNull),
'empty_zero_str' => empty($valZeroStr),
'empty_nonempty_str' => empty($valStr),
'is_int' => is_int($valInt),
'is_string' => is_string($valStr),
'is_numeric_str' => is_numeric($valStr),
'is_array' => is_array($valArr),
'is_iterable_arr' => is_iterable($valArr),
'is_object' => is_object($valObj),
'is_scalar_int' => is_scalar($valInt),
'is_scalar_arr' => is_scalar($valArr),
'is_callable_fn' => is_callable($fn),
'is_null' => is_null($valNull),
'gettype_int' => gettype($valInt),
'gettype_float' => gettype(1.5),
'get_debug_type_int' => get_debug_type($valInt),
'get_debug_type_obj' => get_debug_type($valObj),
];
$typeExpected = [
'isset_int' => true,
'isset_null' => false,
'empty_zero_str' => true,
'empty_nonempty_str' => false,
'is_int' => true,
'is_string' => true,
'is_numeric_str' => true,
'is_array' => true,
'is_iterable_arr' => true,
'is_object' => true,
'is_scalar_int' => true,
'is_scalar_arr' => false,
'is_callable_fn' => true,
'is_null' => true,
'gettype_int' => 'integer',
'gettype_float' => 'double',
'get_debug_type_int' => 'int',
'get_debug_type_obj' => 'stdClass',
];
$cases[] = [
'name' => 'type_checks_and_inspectors',
'passed' => $typeActual === $typeExpected,
'expected' => $typeExpected,
'actual' => $typeActual,
];
// Case 2: intval, floatval, boolval, strval, settype, unset
$mutable = '128';
$setOk = settype($mutable, 'int');
$temp = 'gone';
unset($temp);
$convActual = [
'intval_prefix' => intval('42px'),
'intval_hex' => intval('ff', 16),
'floatval' => floatval('3.14rem'),
'boolval_zero' => boolval('0'),
'boolval_zero_float_str' => boolval('0.0'),
'strval' => strval(2026),
'settype_ok' => $setOk,
'settype_result' => $mutable,
'unset_checked' => isset($temp),
];
$convExpected = [
'intval_prefix' => 42,
'intval_hex' => 255,
'floatval' => 3.14,
'boolval_zero' => false,
'boolval_zero_float_str' => true,
'strval' => '2026',
'settype_ok' => true,
'settype_result' => 128,
'unset_checked' => false,
];
$cases[] = [
'name' => 'type_conversions_and_settype',
'passed' => $convActual === $convExpected,
'expected' => $convExpected,
'actual' => $convActual,
];
// Case 3: serialize / unserialize, var_export, print_r, get_resource_type
$payload = ['lang' => 'PHP', 'major' => 8, 'active' => true];
$ser = serialize($payload);
$unser = unserialize($ser, ['allowed_classes' => false]);
$exported = preg_replace('/\s+/', ' ', trim(var_export(['a' => 1, 'b' => true], true)));
$stream = fopen('php://memory', 'r+');
$resIsResource = is_resource($stream);
$resType = get_resource_type($stream);
fclose($stream);
$serActual = [
'serialized' => $ser,
'roundtrip' => $unser,
'var_export_compact' => $exported,
'is_resource' => $resIsResource,
'resource_type' => $resType,
];
$serExpected = [
'serialized' => 'a:3:{s:4:"lang";s:3:"PHP";s:5:"major";i:8;s:6:"active";b:1;}',
'roundtrip' => ['lang' => 'PHP', 'major' => 8, 'active' => true],
'var_export_compact' => "array ( 'a' => 1, 'b' => true, )",
'is_resource' => true,
'resource_type' => 'stream',
];
$cases[] = [
'name' => 'serialize_unserialize_and_export',
'passed' => $serActual === $serExpected,
'expected' => $serExpected,
'actual' => $serActual,
];
// Case 4: password_hash, password_verify, password_get_info, password_needs_rehash
$secret = 'BlueSky#2026';
$hash = password_hash($secret, PASSWORD_BCRYPT, ['cost' => 10]);
$info = password_get_info($hash);
$pwdActual = [
'verify_right' => password_verify($secret, $hash),
'verify_wrong' => password_verify('WrongPass', $hash),
'algo_name' => $info['algoName'],
'cost' => $info['options']['cost'],
'needs_rehash_same' => password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 10]),
'needs_rehash_higher' => password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 12]),
];
$pwdExpected = [
'verify_right' => true,
'verify_wrong' => false,
'algo_name' => 'bcrypt',
'cost' => 10,
'needs_rehash_same' => false,
'needs_rehash_higher' => true,
];
$cases[] = [
'name' => 'password_hash_and_verify',
'passed' => $pwdActual === $pwdExpected,
'expected' => $pwdExpected,
'actual' => $pwdActual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
- 核对 isset/empty/is_*/gettype/get_debug_type、intval/floatval/boolval/settype、serialize/unserialize 禁用类反序列化与 stream 资源类型,以及 bcrypt cost 10/12 验证与重哈希判断。
所需环境:Windows x64、PHP 8.4 本地 CLI 环境。
当前源码 SHA256:14ff59071bc6569748e9ec158678c62ecdde7542b1c8b26df21f4b49e2a8ac21
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、PHP 8.4 本地 CLI 环境。
ZIP SHA256:3500cbe37102f82f06c8e2919293ffe9be75331ea88d7e5585919aad4c4e866f
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
对来自客户端或不可信存储的序列化字符串直接调用 unserialize($data) 存在什么安全风险?应如何防范?
展开参考解答
不可信的序列化载荷可能实例化任意已加载类并触发 __wakeup()/__destruct() 魔术方法链,导致 PHP 对象注入漏洞;数据交换应优先改用 json_encode()/json_decode(),确需反序列化时必须传入 ['allowed_classes' => false]。