duanniling0018 2015-09-14 10:22
浏览 70
已采纳

为什么字符串在PHP 5.3中的行为类似于数组?

I have this code:

$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;

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.

What's so wrong with my code?

  • 写回答

1条回答 默认 最新

  • drpjdfj618393 2015-09-14 10:26
    关注

    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 |
    

    After 5.3 such offsets will throw a notice, as you can also read in the manual:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画