PHucking Php
I'm not a huge fan of PHP mainly because it's badly engineered (even the API gives you the feeling that it's a mess under the hood (and it's more than just a feeling (but I think they are slowly getting better))).
PHP handles strings as byte arrays so you can access a character by giving it's position in the string like you would access an element of an array:
$string = 'foo';
print $string[2]; // Result: 'o'
PHP also handles arrays as map, so the array index is in fact a key, if you don't provide it it's an increasing number (so you get a 'normal' array), but you can also provide the key yourself (so you get a map):
$array = array('foo' => 'baz');
print $array['foo']; // Result: 'baz'
I ran into a bug in an old version of PHP by mixing the 2 properties of PHP I just talked about:
$string = 'foo';
print $string['bar'];
In a strict language you can't even write something that wrong. In a less strict language you get an exception. In an even less strict language you would expect to get something like null
or false
or 0
. In PHP you get f
(the first character of the string) and your program will happily continue to run… At that point your application is corrupted.
The problem is still here in PHP 5.6 but you also get a warning in your logs: PHP Warning: Illegal string offset 'bar'
, that's all.
Someone opened a bug related to this misbehavior of array brackets with strings 9 years ago in PHP bug tracker and nothing happened.
Comments Add one by sending me an email.