Q:

PHP - isset returns true for non existing array key

I thought I knew everything about php until I bumped into this:
$foo = 'hello';
isset($foo['a']);     // returns false - OK
isset($foo['a']['b']; // returns false - OK
isset($foo['a'][0]);  // returns true! WTF?!

Could anybody explain me the result of the 4th line? Tested with php 5.5.36.

A:

Well, the question is somewhat misleading, because isset returns true for any variable that is not null. Since $foo is a string, and not an array, $foo["a"] gives an Illegal string offset warning. PHP assumes that you meant to cast "a" as an integer offset and does that implicitly, turning $foo["a"] into $foo[0] which gives you the string "h" (the first offset of the string). Since the return value is another string the expression becomes "h"[0], which is just "h" again.
So in other words, $foo["a"][0] where $foo = "hello" is the same thing as $foo[0][0] which gives us "h".
But as far as non-existing array keys, isset would definitely return false since a non-existing key leads to a non-existing value which is implicitly null.

