3 ms·
From here: http://www.php.net/manual/en/migration54.other.php http://www.php.net/manual/en/migration54.other.php > Chained string offsets - e.g. $a[0][0] where
by function_seven 15y ago
From here: http://www.php.net/manual/en/migration54.other.php http://www.php.net/manual/en/migration54.other.php
> Chained string offsets - e.g. $a[0][0] where $a is a string - now work.
Can someone explain why this is? I'm sure I'm misunderstanding, but this seems to mean that the following would work:
$a = "Hello";
echo $a[0]; //"h"
echo $a[0][0]; // also "h"??
What's the point? To avoid failure if I pass a string instead of a 2-dimensional array?
- fooyc 15y agoConsistency. Since $a[0] is a string, you would expect $a[0][0] to work too.
- function_seven 15y agoThanks. Let me rework my original question then. Why the hell didn't it always work? php > $a = "Hello"; php > echo $a[0]; H php > echo $a[0][0]; PHP Fatal error: Cannot use string offset as an array in php shell code on line 1
- tlack 15y agoPHP always seemed to have something weird in the parser about subscripts/array indexes. It's been there since I started using it during PHP3. Another similar fix they performed is making func()[0] work - before you had to do $tmp = func(); $tmp[0]. An old, old bug finally fixed.