Illegal offset type in isset or empty

Description

PHP only uses integers and strings as array index. While certain other types may be converted silently to those types, as null or boolean, objects and arrays yield an illegal type for index.

This error is is specific to isset and empty: these functions are usually very silent, yet an dedicated error message was created for the type check.

This error is reported at compile time when PHP can detect the type then, otherwise, it is reported at execution time.

Example

<?php

$array = [1];

var_dump(isset($array[[]]));

?>

Solutions

  • Convert these types into a string or a integer first.

  • Cast these types into a string or a integer first.