PHP

PHP

Made by DeepSource

Use of an empty [] to read from an array PHP-W1002

Bug risk
Critical

Trying to access an offset without providing a dimension is not allowed. This will emit a fatal error during runtime.

Unlike assignment, an offset without a dimension doesn't work implicitly in array access expressions. Always specify a dimension when accessing an array offset. Not doing so will result in a runtime error, and may even crash the application.

Bad practice

$array = [];

$array[] = 10;
$array[][] = 10;
echo $array[1][]; // invalid: empty dimension when reading the value

Recommended

$array = [];

$array[] = 10;
$array[][] = 10;
echo $array[1][0];  // valid: will echo 10

References