本篇文章给大家带来了关于php8.3的最新相关信息,其中主要介绍了PHP8.3将会添加名为json_validate的函数,那么这个函数有什么用呢?怎么使用它呢?下面一起来看一下,希望对大家有帮助。

PHP 8.3 前瞻:`json_validate` 函数
PHP 8.3 将会添加名为 json_validate 的函数,用于验证传入的字符串是否是合法的 JSON 字符串。
在目前,验证 JSON 字符串的一种通用做法是尝试将其解码,并通过捕获异常来确定。但某些情况下我们可能并不需要获得具体的内容,只需要验证其合法。新的 json_validate 函数相比 json_decode 耗用的内存和资源更少,因为它只分析字符串而不会尝试解码。
函数签名
/** * 验证传入的字符串是否为合法 JSON 字符串 * * @param string $json 准备验证的字符串 * @param int $depth 最大嵌套深度,必须大于 0 * @param int $flags 标志掩码,用于指定行为 * @return bool $json 是合法 JSON 字符串时返回 true,否则返回 false */ function json_validate(string $json, int $depth = 512, int $flags = 0): bool {}
标志 Flags
json_validate 的第三个参数是 flags,用于指定函数的行为。在目前,唯一可用的标志是 JSON_INVALID_UTF8_IGNORE。
该标志在 PHP 7.2 中添加,作为 json_decode 的标志常量,用于忽略对应字符串中的 UTF-8 字符。
json_validate('[1, 2, 3]', flags: JSON_INVALID_UTF8_IGNORE); // true json_validate("[\"\xc1\xc1\",\"a\"]"); // false json_validate("[\"\xc1\xc1\",\"a\"]", flags: JSON_INVALID_UTF8_IGNORE); // true
错误处理
json_validate 本身并不会返回错误码,如果你想要获取具体的错误信息,可用使用 json_last_error 和 json_last_error_msg 获取。
json_validate(""); // false json_last_error(); // 4 json_last_error_msg(); // "Syntax error"
json_validate("null"); // true json_last_error(); // 0 json_last_error_msg(); // "No error"
示例
验证字符串并抛出异常
if (json_validate($_GET['json']) === false) { throw new \JsonException(json_last_error_msg(), json_last_error()); }
替代以前的验证方式
- $value = json_decode($_GET['json'], flags: JSON_THROW_ON_ERROR); + if (!json_validate($_GET['json'])) { + throw new \JsonException(json_last_error_msg(), json_last_error()); + } + $value = json_decode($_GET['json']);
Polyfill 抢先适配
如果你想提前为 PHP 8.3 做适配,以在 8.3 发布的第一时间无缝切换到 json_validate,你可以手动定义一个函数,以在之前的版本中模仿 json_validate 的作用。
if (!function_exists('json_validate')) { function json_validate(string $json, int $depth = 512, int $flags = 0): bool { if ($flags !== 0 && $flags !== \JSON_INVALID_UTF8_IGNORE) { throw new \ValueError('json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE)'); } if ($depth <= 0 ) { throw new \ValueError('json_validate(): Argument #2 ($depth) must be greater than 0'); } \json_decode($json, null, $depth, $flags); return \json_last_error() === \JSON_ERROR_NONE; } }
由于此函数内部依然使用 json_decode,所以其实际上并没有性能上的改进,只是提供了和 json_validate 相似的接口。
推荐学习:《PHP视频教程》
Atas ialah kandungan terperinci PHP8.3要有新函数了!(json_validate函数说明). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!
iPhone 16 dijangka masa pelancaran
Analisis prospek syiling ICP
akhiran peti mel google
Mengapa video dalam ppt tidak boleh dimainkan?
Apa yang berlaku apabila phpmyadmin tidak dapat mengaksesnya?
504 Gateway Masa tamat sebab dan penyelesaian
Bagaimana untuk mengubah suai nama fail dalam linux
Kedudukan terkini sepuluh bursa teratas dalam bulatan mata wang