我有这个代码:
$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
它在 PHP 5.5 中运行良好,但在 PHP 5.3 中,当 $host
包含类似 pjba01
的字符串时,条件返回 true。它返回$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.
关于php - 为什么字符串在 PHP 5.3 中表现得像数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32562436/