PHP Undefined index — Array key does not exist
Warning: Undefined index / array key
Verified against PHP 8.3 documentation, PHP migration guide 7→8 (array key warnings), PHP filter_input documentation · Updated June 2026
> quick_fix
Use the null coalescing operator (??) to provide a default value: `$value = $array['key'] ?? 'default';`. This replaces the three-line isset() check with a single expression.
<?php
// BROKEN: triggers warning if 'name' key doesn't exist
$name = $_POST['name'];
// FIXED option 1: null coalescing (PHP 7+)
$name = $_POST['name'] ?? '';
// FIXED option 2: isset check
$name = isset($_POST['name']) ? $_POST['name'] : '';
// FIXED option 3: array_key_exists (distinguishes missing from null)
if (array_key_exists('name', $_POST)) {
$name = $_POST['name']; // may still be null
} else {
$name = '';
}
// Nested keys with null coalescing
$city = $_POST['address']['city'] ?? 'Unknown';What causes this error
PHP triggers an 'Undefined index' notice (PHP 7) or 'Undefined array key' warning (PHP 8) when you access an associative array with a key that does not exist. Common scenarios: accessing $_POST/$_GET/$_SESSION keys that were not submitted, accessing a JSON-decoded array key that is absent from the data, or accessing a database result column that was not selected.
How to fix it
- 01
step 1
Use null coalescing (??) as the default fix
PHP 7+ null coalescing: `$val = $arr['key'] ?? 'default'`. This returns 'default' if the key does not exist OR if the key exists but its value is null. It replaces the verbose `isset($arr['key']) ? $arr['key'] : 'default'` pattern.
- 02
step 2
Use isset() when you need to distinguish null from missing
`isset($arr['key'])` returns false if the key is missing OR if the value is null. If you need to detect a key that exists but is explicitly set to null, use `array_key_exists('key', $arr)` instead.
- 03
step 3
Filter and validate $_POST/$_GET at request entry
Do not scatter null-coalescing all over the codebase. Validate the request array once at entry and extract only expected keys with defaults: `$input = filter_input_array(INPUT_POST, ['name' => FILTER_DEFAULT, 'age' => FILTER_VALIDATE_INT]);`
- 04
step 4
Use array_key_exists for strict presence checks
When a missing key means a programming error (not just absent input), use `array_key_exists('key', $arr)` and throw an exception or log an error if it is missing, rather than silently using a default.
- 05
step 5
Enable error reporting in development
Set `error_reporting(E_ALL)` and `display_errors = On` in php.ini for development. Undefined index is a notice/warning in PHP 7 but became a warning in PHP 8 — in both cases it should be treated as a bug and fixed, not suppressed.
How to verify the fix
- Submit a form or make an API call that omits the key — no warning should appear
- Check that the default value used is appropriate (empty string, 0, null, etc.)
- Enable E_ALL error reporting and confirm no remaining Undefined index warnings
Why Undefined index happens at the runtime level
PHP arrays are implemented as hash maps that return null and emit a diagnostic when accessing a non-existent key. Prior to PHP 8, this was only a Notice (E_NOTICE) — easily ignored — which is why legacy codebases are full of unguarded array accesses. PHP 8 upgraded this to E_WARNING to push developers toward explicit key checking. The null coalescing operator (??) introduced in PHP 7 was specifically designed to address this pattern, providing a language-level default mechanism for absent array keys.
Common debug mistakes for Undefined index
- Accessing $_POST['field'] without checking if the form actually submitted that field
- Accessing json_decode() result keys without checking if the key exists in the JSON payload
- Using $row['column'] on a database result without checking if the column was included in the SELECT
- Suppressing with @ instead of fixing — @ hides the warning but returns null, causing silent downstream bugs
When Undefined index signals a deeper problem
Undefined index warnings throughout a codebase are a sign that input validation is not centralized. Every controller or function defensively re-checks whether a key exists, producing duplicated `isset()` guards scattered everywhere. The architectural fix is a request value object or DTO that validates and hydrates all expected fields at the request boundary, throwing a validation exception if required fields are missing. The rest of the codebase then works with typed, guaranteed-present values rather than raw arrays.
Editor's take
Undefined index (or its PHP 8.0+ equivalent, 'Undefined array key') fires when you try to read an array key that doesn't exist. In PHP 7.x this was a Notice (often suppressed in production), but PHP 8.0 elevated it to a Warning, and strict frameworks treat warnings as exceptions. This change broke a lot of legacy code that relied on reading missing keys and getting `null` silently. The most common production cause: accessing `$_POST['field']` or `$_GET['param']` without checking if the key exists first — when a user submits a form with a missing field or navigates to a URL without the expected query parameter. The defensive patterns are `$_POST['field'] ?? 'default'` (null coalescing operator, PHP 7+), `array_key_exists('field', $_POST)`, or `isset($_POST['field'])`. In Laravel, the `$request->input('field', 'default')` method handles this cleanly. The subtlety: `isset()` returns false for keys that exist but have a `null` value, while `array_key_exists()` returns true — this distinction matters when null is a valid value in your data. In API integrations, this error often signals that a third-party response changed its schema — a field you expected is no longer present. The robust pattern is to validate the entire response structure upfront with a schema validator rather than accessing nested keys optimistically. PHP 8.0's promoted constructor properties and named arguments reduce this error class significantly in well-typed codebases.
By Bikram Nath · Curator · Updated June 2026
Frequently asked questions
Is Undefined index a fatal error or a warning?
In PHP 7 it is a Notice (E_NOTICE). In PHP 8 it was upgraded to a Warning (E_WARNING). It is not fatal — script execution continues. However, it should be treated as a bug because accessing a non-existent key returns null, which may silently corrupt data downstream.
What is the difference between Undefined index and Undefined offset?
Undefined index occurs when accessing an associative array with a string key that does not exist. Undefined offset occurs when accessing a numeric array with an integer index that does not exist (e.g., $arr[5] on a 3-element array). Both are the same underlying issue — missing array key — just with string vs integer keys.
Should I use @ to suppress the warning?
No. The @ error suppression operator hides the warning but does not fix the underlying issue. The null is still returned and may cause silent data corruption. Fix the code to check for the key's existence explicitly.