Why does PHP not complain when I treat a null value as an array like this?

There are three types which it might be valid to use the array derefence syntax on:

  • Arrays
  • Strings (to access the character at the given position)
  • Object (objects implementing the ArrayAccess interface)

For all other types, PHP just returns the undefined variable.

Array dereference is handled by the FETCH_DIM_R opcode, which uses zend_fetch_dimension_address_read() to fetch the element.

As you can see, there is a special case for NULLs, and a default case, both returning the undefined variable.

Leave a Comment