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

**Code:** `Array offset on null` · **Category:** PHP · **Seen on:** PHP

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/php/trying-to-access-array-offset-on-null)
