为什么字符串在 PHP 5.3 中表现得像数组?
Why do strings behave like an array in PHP 5.3?
我有这个代码:
$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
它在 PHP 5.5 中运行良好,但在 PHP 5.3 中,条件 returns 为真,而 $host
包含类似 pjba01
的字符串。是returns$tierHosts['host']
的第一个字母,即p
.
我的代码有什么问题?
You can access strings like an array 和之前的 PHP 5.4 偏移量,如您的 name
被静默转换为 0,意味着您访问了该字符串的第一个字符:
character | p | j | b | a | 0 | 1 |
-----------------------------------
index | 0 | 1 | 2 | 3 | 4 | 5 |
5.3以后这样的偏移量会抛出一个通知,你也可以在手册中看到:
As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Previously an offset like "foo" was silently cast to 0.
我有这个代码:
$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
它在 PHP 5.5 中运行良好,但在 PHP 5.3 中,条件 returns 为真,而 $host
包含类似 pjba01
的字符串。是returns$tierHosts['host']
的第一个字母,即p
.
我的代码有什么问题?
You can access strings like an array 和之前的 PHP 5.4 偏移量,如您的 name
被静默转换为 0,意味着您访问了该字符串的第一个字符:
character | p | j | b | a | 0 | 1 | ----------------------------------- index | 0 | 1 | 2 | 3 | 4 | 5 |
5.3以后这样的偏移量会抛出一个通知,你也可以在手册中看到:
As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Previously an offset like "foo" was silently cast to 0.