Error fix · Developer errors

PHP Warning: Undefined array key - What It Means and How to Fix It

Fix PHP's Undefined array key warning with isset(), the null coalescing operator and array_key_exists(), and learn why PHP 8 replaced Undefined index.

Error messageWarning: Undefined array key

By SiteDownCheck Editorial · Updated November 4, 2026 · 4 min read

Warning: Undefined array key "name" (or Undefined array key 3 for a numeric index) means your code read an element from an array that does not exist. It is a warning, so the script keeps running and the missing value is treated as null, but it signals a real assumption that failed. It also fills logs, breaks JSON responses when warnings are printed to the page, and hides worse bugs.

Warning: Undefined array key "email" in /var/www/app/register.php on line 14

In PHP 7 and earlier the same situation produced a notice: Notice: Undefined index: email (for string keys) or Undefined offset: 3 (for integer keys). PHP 8.0 unified the wording and raised the severity from notice to warning, which is why sites that were quiet for years suddenly start logging thousands of lines after an upgrade.

Why it happens

  1. Missing input. A form field, query parameter or header was not sent. $_POST['email'] does not exist for an unchecked checkbox, an empty disabled field, or a request from an API client that omitted it.
  2. Optional JSON keys. json_decode() returned an array without a key the code expects, or returned null for invalid input.
  3. Database rows. A column was renamed, or a SELECT no longer returns it. Case matters: $row['Name'] is not $row['name'].
  4. Off-by-one indexing. Looping to count($a) instead of count($a) - 1, or reading $parts[1] after explode() on a string with no delimiter.
  5. Config arrays and superglobals that differ between environments, such as $_SERVER['HTTPS'] or $_SERVER['HTTP_REFERER'], which are not always set.
  6. Plugin or theme code written for PHP 7 where notices were hidden.

The fixes

Null coalescing: simplest for defaults

$email = $_POST['email'] ?? '';
$page  = (int) ($_GET['page'] ?? 1);
$city  = $data['address']['city'] ?? null;   // works through nested levels

The ?? operator never raises the warning, even when intermediate keys are missing.

isset() or array_key_exists() for logic

if (isset($row['status'])) { /* key exists and is not null */ }
if (array_key_exists('status', $row)) { /* key exists even if null */ }

The difference matters when null is a legitimate value. isset() returns false for a key whose value is null; array_key_exists() returns true.

Validate at the boundary

For user input, validate once, then work with a clean structure:

$required = ['email', 'name'];
foreach ($required as $key) {
    if (!isset($_POST[$key]) || $_POST[$key] === '') {
        http_response_code(422);
        exit("Missing field: $key");
    }
}

When consuming JSON, check it first with the JSON Validator and look at its shape with the JSON Formatter. In code, test json_last_error() === JSON_ERROR_NONE before using the result.

Fix indexes in loops

foreach ($items as $i => $item) { ... }          // no manual index
$parts = explode(':', $value, 2);
$host = $parts[0];
$port = $parts[1] ?? '80';

Deal with a changed query

If the column disappeared, fix the query or the mapping. Log array_keys($row) once to see what is available.

What not to do

  • Do not hide the warning with @. @$arr['x'] still evaluates the same way, costs performance, and buries the signal.
  • Do not lower error_reporting to hide it in production as the only fix. It is fine to log rather than display, but fix the code too.
  • Do not edit vendor code in place without a plan. Update the plugin, or patch it with a proper override.

Production settings

Warnings printed into a page can break redirects (see PHP headers already sent) and corrupt JSON or XML output, producing odd client errors. In production:

display_errors = Off
log_errors = On
error_log = /var/log/php/error.log
error_reporting = E_ALL

In development keep display_errors = On and error_reporting = E_ALL so you see every warning.

WordPress

Many Undefined array key lines come from a plugin or theme. The path in the message names the culprit. Update it, check its support forum, or disable it. In wp-config.php you can send warnings to the log without showing them:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

Switch WP_DEBUG back off when you finish. See WordPress plugin conflict troubleshooting.

Verify

Reproduce the request that triggered the warning, then confirm the log stays clean. Use the HTTP Status Checker to make sure the page still returns the expected status, and keep an Uptime Monitor with a keyword check on the page so a template error is noticed. The Website Health Check provides a quick outside view.

Next steps

FAQ

What is the difference between Undefined index and Undefined array key?

They are the same condition. PHP 8.0 changed the wording and made it a warning rather than a notice.

Does the warning stop my script?

No. The expression evaluates to null and execution continues, which is why wrong results can follow quietly.

Is isset() or ?? better?

Use ?? to provide a default value, and isset() or array_key_exists() for branching logic.

Why do I see it only on the live server?

Environments differ in input data, PHP version and error_reporting. Production may log warnings that development hides.

Get notified when this fix updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.