Using null as an array offset is deprecated, use an empty string instead

Description

Avoid using null as an array index. PHP converts it silently to an empty string anyway.

Example

<?php

$array = ['a' => 2];
$array[null] = 3;

?>

Solutions

  • Replace the null with an empty string.

  • Check the value before using it as an array index.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See NullAsArrayOffset.

Static Analysis

This error may be tracked down with the following static analysis rules: Arrays/NullAsIndex.