Array offset on null PHP Origin: Application & API OSI layer 7 · Application Last updated 14 Oct 2026

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.

Exact message you may see Trying to access array offset on value of type nullTrying to access array offset on value of type boolarray offset on value of type
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 not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ArrayIndexOutOfBoundsException — java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 (Java)ArrayIndexOutOfBounds…invalid array length — RangeError: Invalid array length (JavaScript)invalid array lengthObject.keys null — TypeError: Cannot convert undefined or null to object (JavaScript)Object.keys nullreading length — TypeError: Cannot read properties of undefined (reading 'length') (JavaScript)reading lengthreduce empty — TypeError: Reduce of empty array with no initial value (JavaScript)reduce emptyTypeError not iterable — TypeError: x is not iterable (JavaScript)TypeError not iterableUndefined array key — Warning: Undefined array key / Undefined index (PHP)Undefined array keyUndefined array key — Laravel/PHP 8: ErrorException: Undefined array key "name" (PHP 8) / Undefined index: name (PHP 7) (Laravel)Undefined array keyArray off…

Explore the map of this error Full error map →

🧠 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.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.