Why do strings behave like an array in PHP 5.3?

前端 未结 1 1002
既然无缘
既然无缘 2020-12-17 10:45

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 th

相关标签:
1条回答
  • 2020-12-17 11:18

    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.

    0 讨论(0)
提交回复
热议问题