php - 为什么字符串在 PHP 5.3 中表现得像数组?

标签 php arrays string php-5.3 php-5.4

我有这个代码:

$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/

相关文章:

php - PHP中删除字符串中与字母、数字、特定符号、汉字不匹配的字符

php - Jenkins pdepend插件错误

php - 如何使用 PHP SDK 和图形 API 将事件发布到 Facebook 粉丝页面?

javascript - 用 d3.js/c3.js 替换图表数据集

c# - CSV 文件到位图

python - 使用正则表达式 Pandas 改变年龄

python - 将 python 中的字符串向后分成两部分

php - 添加 jquery 时,即使使用文字标签,tpl 文件也不起作用

java - jsoup 403 错误。简单的网页工作正常

javascript - 我的正则表达式在 Node.js 中不起作用?