我想解决 8.1 中一些已弃用的错误。
PHP Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in...
//explode uids into an array
$comp_uids = explode(',', $result['comp_uids']);
Run Code Online (Sandbox Code Playgroud)
在这种情况下 $result['comp_uids'] 为空,这就是显示 null 错误的原因。我不确定他们为什么不赞成这种能力,但是建议进行哪些更改来避免这种情况?我看到类似的情况strlen(): Passing null to parameter #1 ($string) of type string is deprecated以及其他一些使用 8.1 的情况。
Bar*_*mar 48
如果值为 ,则使用 null 合并运算符将值默认为空字符串null。
$comp_uids = explode(',', $result['comp_uids'] ?? '');
Run Code Online (Sandbox Code Playgroud)
小智 14
只需将参数转换为字符串,即可将 null 转换为 ''。
$comp_uids = explode(',', (string)$result['comp_uids']);
Run Code Online (Sandbox Code Playgroud)
没有必要爆炸 null,你事先知道它不会返回匹配项。如果您确实想要['']结果,则将其明确化会更直观:
$comp_uids = $result['comp_uids'] !== null
? explode(',', $result['comp_uids'])
: [''];
Run Code Online (Sandbox Code Playgroud)
我仍然发现这对于其他程序员来说有点违反直觉。恕我直言,没有找到 UID 的概念最好用空数组来表示,如果您也可以期望空字符串,那么它们很可能与以下内容一起处理null:
$comp_uids = $result['comp_uids'] != ''
? explode(',', $result['comp_uids'])
: [];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88139 次 |
| 最近记录: |