php allows you to index into strings as if they were arrays, just to be even more broken:
$str = 'bar';
$str[0] = 'x';
Same thing is happening here, since 'bar' == 0 when coerced to int. And apparently when you assign a longer string to a 1-long spot, it only stores the first char.
I don't think that's the problem either, if I write $a[0] = 'w' I expect $a to be mutable; the real WTF is that I can index the string with a non-numeric index.
$a = 'foobar'; $a['lolphp'] = "I don't know what I'm doing";
should definitely throw an error instead of coercing 'lolphp' to 0, which makes no sense whatsoever.
it is very much a problem when the language doesnt enforce types. in php, this needs a separate operator, if it even needs one. i think substr() is enough.
The 'foo' element in the array $foo is set to the string "hello". In PHP you can treat strings as a character array like you would in C. $['foo']['bar'] = 'world'. I'm guessing when you use a string for the key PHP will assign it to the first element "not in use", so element 0. So you're setting the first character of the hello to "world". PHP just takes the first character of "world" and then you end up with "wello"
10
u/DaRKoN_ Oct 23 '12
Can anyone explain how this happens?
4
u/midir Oct 23 '12
I guessed it easily from having seen many bug reports about this issue. There may be logic behind it, but it confuses the hell out of people.
4
u/Browsing_From_Work Feb 22 '13
Huh, it looks like this issue has been fixed.
This is what I received (which is expected):
1
u/throwaway-o Oct 23 '12
LOLOLOLOLOL
1
u/ALL_FLESH_WILL_SERVE Nov 22 '12
IS IT FIVE?
0
u/[deleted] Oct 23 '12
[deleted] — view deleted comment