Check the following code snippets. They use square bracket syntax to index data in an array and a string.
$array = array(1,2,3);
>> Notice: Undefined index: foo
$string = '123';
If you access a non existing index in an array you will be notified about a non existing index and NULL is returned. That’s fine so far. But what if you’re doing the same with a string? Is a string an array of characters as you might expect?
Unfortunately not in PHP. A string is a string and accessing a non existing index in a string is a different thing. PHP obviously converts the string index “foo” into an integer since strings have only numerical indices. Casting “foo” into integer is 0 and the character at position 0 is returned – without any notice. A string is not an array of characters.
As you may read here http://bugs.php.net/bug.php?id=44914 this is also fine for the creators of PHP. For me, I’m sorry, it’s a reason to stop using PHP (if I could at least). Implicit type casts in PHP are a story on its own and notoriously leading to subtle and hard to find bugs. But this one is beyond belief.
The only way to protect you from a catastrophe is to check at any and every usage of arrays if you really have an array. Type hint arrays. Use is_array() excessively. Write thousands of tests.
Better choice – use a well designed programming language. A better supported at least.