Skip to main content

Solved | PHP error Trying to access array offset on value of type null

Problem: PHP errors out when running Trying to access array offset on value of type null

Cause Analysis:

The meaning of this error is: trying to access the array offset of a value of type null , which means that one of the values ​​becomes nul, causing an error. This error only occurred when the PHP version was 7.4. The new version of the php interpreter will directly report an error for null type subscript access. For example:

$b = NULL;
$a = $b['key'] ? $b['key'] : 0;

The above statement, $b, reports an error when it is null.

There are two solutions:

Option 1: Lower the PHP version. You can try to lower the PHP version to below 7.4. The problem will disappear automatically.

Option 2: Modify the code. For example, you can change

$b = NULL;
$a = $b['key'] ? $b['key'] : 0;

change into:

$b = NULL;
$a = isset($b['key']) && !empty($b['key']) ? $b : 0;(Just add an additional judgment.)