Ffile2fix
Sign in Get started

How to fix "Trying to access array offset on value of type null"

Warning: Trying to access array offset on value of type null in /home/user/public_html/wp-content/plugins/myplugin/admin.php on line 77

The code reads $var['key'], but $var is not an array at all; it is null, false or a number. This is different from 'Undefined array key': the whole variable is missing, not just one key.

Also appears as: Warning: Trying to access array offset on value of type bool in ... · Warning: Trying to access array offset on null · Notice: Trying to access array offset on value of type int (PHP 7.4)

Common causes

  • A function returned false or null on failure (mysqli_fetch_assoc() after the last row, get_option() for a missing option, json_decode() on invalid JSON)
  • A database query found no row, so $row is null or false
  • Nested data where an intermediate level is missing ($data['user']['address'])
  • Old plugins and themes written before PHP 7.4, which silently returned null here
  • Reading from a variable that was never initialized as an array

How to fix it

  1. Identify the variable being indexed. On the reported line, find the variable before [ and var_dump() it to see whether it is null, false or another scalar.
  2. Trace where it was set. Find the function call or query that produced it and check its return value on failure in the PHP or WordPress docs.
  3. Check before indexing. Use is_array($row) or if ($row) before reading keys, or the null coalescing operator: $row['name'] ?? '' works even if $row is null.
  4. Validate decoded data. After json_decode($json, true), check json_last_error() or use JSON_THROW_ON_ERROR so invalid JSON does not become null silently.
  5. Update outdated plugins and themes. If the path is inside wp-content/plugins or themes, update that component. This warning is common in code written for PHP 7.3 and older.
  6. Log instead of display in production. Set WP_DEBUG_DISPLAY to false (or display_errors = Off) and review the log while you fix the code.

PHP

$row = $stmt->fetch(PDO::FETCH_ASSOC);   // false if no row
if (!is_array($row)) {
    return null;
}
$name = $row['name'];

// or, for nested optional data:
$city = $data['user']['address']['city'] ?? 'Unknown';

How to stop it happening again

  • Check return values of functions that can return false or null
  • Use ?? for optional nested data
  • Decode JSON with JSON_THROW_ON_ERROR

Frequently asked questions

How is this different from 'Undefined array key'?

'Undefined array key' means the array exists but lacks that key. 'Array offset on null' means the variable itself is null, false or a number, not an array.

Why did it appear after upgrading PHP?

PHP 7.4 started reporting this as a notice and PHP 8 raised it to a warning. Earlier versions silently returned null, so the bug was invisible.

Does ?? work if the variable itself is null?

Yes. $row['name'] ?? 'default' returns the default without a warning even when $row is null or undefined.