Error guides

Trying to access array offset on null

You used [] on null, often after a failed json_decode or a missing config key. Check the source of the value, or read it with data_get.

Message
Trying to access array offset on null

What it means

You used [] on a value that is not an array, as in $data['user'] when $data is null. PHP 8.5 gives the warning Trying to access array offset on null and the expression becomes null. The warning names the type you hit, so false and an integer give Trying to access array offset on false and Trying to access array offset on int. Laravel turns every warning into an ErrorException, so in a Laravel app the request stops on that line.

It differs from Undefined array key "user": there the array exists and lacks the key, here there is no array at all. PHP 7.4 added this check as a notice worded Trying to access array offset on value of type null, PHP 8.0 made it a warning, and PHP 8.3 shortened it to the current wording.

Why it happens

  • json_decode() failed. It returns null when the JSON cannot be decoded, so json_decode($body, true)['user'] fails on a truncated payload.
  • A config key does not exist. config('services.acme')['key'] fails when services.acme is not defined, because config() returns null for it.
  • A JSON column is null. With an array cast, $post->meta['seo']['title'] fails for a row whose meta is null.
  • Input is missing. $request->input('address')['city'] fails when the request has no address.
  • A function returned false on failure. fgetcsv() returns false at the end of a file, so $row[0] gives the false version.

How to fix it

Decode with JSON_THROW_ON_ERROR, so bad JSON fails at the decode with a JsonException (message Syntax error) instead of a warning further down:

<?php

try {
    $data = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
    throw new \InvalidArgumentException('The payload is not valid JSON.', previous: $e);
}

Valid JSON can still be null or lack the key you read, so check the shape of $data after decoding as well.

If you only need to know whether a string is valid JSON, json_validate() (PHP 8.3 and later) answers that. The manual advises against calling it right before json_decode(), which validates the string again.

When a value is optional, read it with a dotted path. data_get(), Arr::get(), config() and $request->input() return the default instead of warning:

<?php

use Illuminate\Support\Arr;

$title = data_get($post->meta, 'seo.title', 'Untitled');
$userId = Arr::get($payload, 'user.id');
$key = config('services.acme.key');
$city = $request->input('address.city');

When the value is required, a default hides the bug. Validate the input instead, so the request fails with a validation error before your code runs:

<?php

$validated = $request->validate([
    'address.city' => ['required', 'string'],
]);

For a missing config key, add it to the config file and check the environment variable behind it. For a function that returns false, compare with false before you use the result:

<?php

while (($row = fgetcsv($handle, escape: '')) !== false) {
    $firstColumn = $row[0];
}

Find it in production

In Laravel with APP_DEBUG=false, the visitor gets a 500 "Server Error" page. The log in storage/logs/laravel.log shows an ErrorException with the message, file and line. Laravel reports it, because ErrorException is not on the handler's $internalDontReport list, so Ranetrace's error monitoring records it with the stack trace and the request it happened on.

In plain PHP the warning does not stop the script. With display_errors off, the page returned 200 with the value empty, and the warning reached only the PHP error log, twice for $data['user']['id']: once for each [].

Reproduced on PHP 8.5, Laravel 13.45, 5 December 2026.

Start monitoring today

Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.

Start your 30-day free trial From $9 a month. No credit card required.