Array offset on null 🐘 PHP

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

Code used $x['key'] where $x is null — usually a lookup or function that returned nothing.

Seen on: PHP

Meaning

PHP 7.4+ warns when you index into null, bool or int. The variable you think is an array is actually null because a query returned no row, a function returned null, or a key was missing.

Common causes

  • Database fetch returned no row (fetch() → false/null)
  • Function returned null on failure
  • json_decode failed and returned null
  • Nested array key missing one level up

⚡ Quick fix

  1. Check the value before indexing
  2. Use the null coalescing operator: $row['name'] ?? ''
  3. Handle "not found" explicitly

Detailed fix by platform

PHP

  1. $user = $st->fetch(); if (!$user) { http_response_code(404); exit; }

How to diagnose

  1. Variable — Which variable is null?
  2. Source — What returned it — query, function, JSON?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.