Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP: Trying to access array offset on value of type null

ErrorTrying to access array offset on value of type null

Short answer

You wrote $x['key'] while $x is null (or false or an int). The query returned no row, a function failed, or a key higher up was missing. PHP 7.4 gave a notice, PHP 8 gives a warning. Find what produced null, then guard it with a check or the ?? operator.

  • Applies to PHP 7.4, PHP 8.3, PHP 8.4, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump the variable on the line above the failing one, with the line number. It prints NULL or false.

    var_dump($row); // or dd($row) in Laravel
  2. 2Match what you see, then jump to its fix

    1. 1The same code works for some ids and fails for ids that do not existA query matched nothing, so the result is null or falseFix
    2. 2Reads like $data['user']['name'] fail, and the first level existsA nested key is missing in decoded dataFix
    3. 3The error showed up after a PHP upgrade, with no code changeCode written for PHP 7.3 now runs on 7.4 or laterFix

What does the error look like?

  • Trying to access array offset on value of type null
  • Warning: Trying to access array offset on value of type null
  • Trying to access array offset on value of type bool

Cause 1 of 3

A query matched nothing, so the result is null or false

1Check

Eloquent's first() and the mysqli fetch functions give null when there is no row. An array read on that result raises the warning.

$user = User::where('email', $email)->first();
var_dump($user);

2Fix

Handle the empty result before reading from it. In Laravel, firstOrFail() turns it into a 404, and the nullsafe operator reads properties safely.

$user = User::where('email', $email)->firstOrFail();

// plain PHP array result
$name = $row['name'] ?? null;

// object result (PHP 8.0+)
$city = $user?->address?->city;

3Verify

Request an id that does not exist and confirm you get a clean 404 or null, not a warning.

php artisan tinker --execute="var_dump(App\Models\User::find(0));"

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A nested key is missing in decoded data

1Check

If $data['user'] is null, reading ['name'] from it raises the warning. Dump the middle level.

var_dump($data['user'] ?? 'missing');

2Fix

Use ?? on the whole chain. It gives the fallback when any part is null or undefined, and raises nothing.

$name = $data['user']['name'] ?? 'guest';

3Verify

Feed it data without the user key.

php -r '$data=[]; var_dump($data["user"]["name"] ?? "guest");'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Code written for PHP 7.3 now runs on 7.4 or later

1Check

Before PHP 7.4, reading a key from null gave no message. From 7.4 it is a notice, from 8.0 a warning, so old code starts to report problems that were always there.

php -v

2Fix

Do not switch the warning off. Fix each place with a check or ??, because the null was always the real bug.

// before
$total = $order['totals']['sum'];
// after
$total = $order['totals']['sum'] ?? 0;

3Verify

Run the page with error reporting on and see no new warnings.

php -d error_reporting=E_ALL -d display_errors=1 index.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Treat any first() or find() result as nullable and handle that case on the next line.
  • Add return types such as ?array so null is visible in the signature.
  • Run a static analyser (PHPStan or Larastan) in CI; it finds array reads on nullable values.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.