琴弦为什么表现得像在PHP 5.3数组? [英] Why do strings behave like an array in PHP 5.3?
问题描述
我有这样的code:
$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
它的正常工作在PHP 5.5,但在PHP 5.3的条件下返回true,而 $主机
包含像 pjba01 $ C $字符串C>。它返回的第一个字母
$ tierHosts ['主机']
,即 P
。
It's working fine in PHP 5.5, but in PHP 5.3 the condition returns true while $host
contains a string like pjba01
. It returns the first letter of $tierHosts['host']
, that is, p
.
这有什么错我的code?
What's so wrong with my code?
推荐答案
的您可以访问字符串像数组和以前的PHP 5.4偏移喜欢你的名称
被静静地铸造为0,意味着你访问该字符串的第一个字符:
You can access strings like an array and prior PHP 5.4 offsets like your name
were silently casted to 0, means you accessed the first character of that string:
character | p | j | b | a | 0 | 1 |
-----------------------------------
index | 0 | 1 | 2 | 3 | 4 | 5 |
在5.3这样的偏移将抛出一个通知,你还可以在手册中读到:
After 5.3 such offsets will throw a notice, as you can also read in the manual:
随着PHP 5.4字符串偏移量必须要么是整数或整数样的字符串,否则警告将被抛出。 previously一个像富偏移悄悄转换为0。
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 5.3数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!