Ffile2fix
Sign in Get started

Fix "Warning: Undefined array key" in PHP 8

Warning: Undefined array key "id" in /var/www/html/edit.php on line 8

Your code read an array element that does not exist, for example $_GET['id'] when no id was sent. PHP 8.0 raised this from a notice ("Undefined index") to a warning, so old code suddenly floods logs or pages. The fix is to check the key exists or supply a default with ??.

Also appears as: Warning: Undefined array key 0 in /var/www/html/list.php on line 22 · PHP Warning: Undefined array key "HTTP_REFERER" · Notice: Undefined index: id · Warning: Trying to access array offset on value of type null

Common causes

  • Reading $_GET, $_POST or $_SERVER values that were not sent with the request
  • Code written for PHP 7 that relied on notices being hidden
  • A typo or different case in the key name
  • Numeric index past the end of the array, such as $parts[1] after an explode() with no delimiter found
  • An API or database result missing a field you expected

How to fix it

  1. Use the null coalescing operator. Replace $id = $_GET['id']; with $id = $_GET['id'] ?? null; or a sensible default. The ?? operator does not raise a warning for missing keys.
  2. Check before using. Wrap logic in if (isset($data['key'])) { ... }. Use array_key_exists('key', $data) when a null value is valid and must be distinguished from a missing key.
  3. Validate input explicitly. For request data use filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT), which returns null when the parameter is missing and false when it is invalid.
  4. Fix the data source. Use var_dump(array_keys($data)) or print_r to see which keys actually exist. Correct typos or handle the case where an API omits the field.
  5. Hide warnings in production while you fix them. Set display_errors = Off and log_errors = On in production so visitors never see the warning. This does not fix the bug, so keep working through the log.

Safe reads in PHP 8

<?php
$id    = $_GET['id'] ?? null;          // default if missing
$page  = (int) ($_GET['page'] ?? 1);
$ref   = $_SERVER['HTTP_REFERER'] ?? '';

if (array_key_exists('email', $row)) {
    // key exists, value may be null
}

[$user, $domain] = array_pad(explode('@', $email, 2), 2, null);

How to stop it happening again

  • Run tests with error_reporting = E_ALL so warnings show up during development
  • Use ?? defaults for all request and config values
  • Use typed DTOs or validated arrays for API responses
  • Use static analysis such as PHPStan or Psalm to catch unsafe array access

Frequently asked questions

Is 'Undefined array key' the same as 'Undefined index'?

Yes. PHP 8.0 renamed the notice 'Undefined index' and 'Undefined offset' to the warning 'Undefined array key' and made it a higher severity.

Does this warning break my site?

A warning does not stop the script, but the code continues with null, which can cause wrong results or a later fatal error. If display_errors is on, the text can also break redirects with 'headers already sent'.

Should I use @ to silence it?

No. The @ operator hides the symptom and makes debugging harder. Use ?? or isset(), which are just as short and are explicit about the default.