"Trying to access array offset on value of type X" in PHP — What It Means and How to Fix It

What the PHP error "Trying to access array offset on value of type X" means and how to fix it.

Published October 31, 2026

You used array syntax ($x['key']) on a value that is not an array, such as null, bool or int. The result is null.

What it means and why it happens

Typical causes: a function returned null or false instead of an array (failed query, missing key), or JSON decoding failed.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

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

Practical examples

Code that triggers it

<?php
$row = $stmt->fetch();      // false when no row
echo $row['name'];

The fix

<?php
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) { exit('No record'); }
echo $row['name'];

Safe access

echo $data['user']['name'] ?? 'Guest';

Common mistakes

  • Not checking fetch() results.
  • Ignoring json_decode errors.
  • Assuming nested keys exist.

Key takeaways

  • Trying to access array offset on value of type null: You used array syntax ($x['key']) on a value that is not an array, such as null, bool or int.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Why is it a warning?

PHP continues with null, but the data is not what you expected.

How do I find which line caused Trying to access array offset on value of type null?

The error message and stack trace show the file and line; start there and work backwards.

More PHP articles