PHP: Trying to access array offset on value of type null
Trying to access array offset on value of type nullShort 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.
Fix it fast
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 Laravel2Match what you see, then jump to its fix
- 1
The 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
Reads like $data['user']['name'] fail, and the first level existsA nested key is missing in decoded dataFix - 3
The error showed up after a PHP upgrade, with no code changeCode written for PHP 7.3 now runs on 7.4 or laterFix
- 1
What does the error look like?
Trying to access array offset on value of type nullWarning: Trying to access array offset on value of type nullTrying 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
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
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 -v2Fix
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.phpConfirmed by php.net
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
PHP says Illegal string offset or Cannot access offset of type string on string
Warning: Illegal string offset "1x"
You used an array key on a value that is a string.
3 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix