Undefined index
PHP Programming Language
Severity: MinorWhat Does This Error Mean?
PHP Notice: Undefined index means you are trying to access an array key that does not exist. For example, reading $_POST['email'] when the form was submitted without an email field. This is a notice-level message — not a fatal error — but it indicates your code is reading from an array without checking if the key exists first. The fix is to check for the key with isset() or array_key_exists() before reading it.
Affected Models
- PHP 7.4
- PHP 8.0
- PHP 8.1
- PHP 8.2
- PHP 8.3
Common Causes
- Accessing $_POST, $_GET, or $_SESSION keys that were not submitted or set in the current request
- Reading an array index that only exists under certain conditions without checking first
- Iterating an array and accessing a key by name that some elements do not have
- Using a variable as an array key that happens to not match any key in the array
- Accessing database result array fields by name when the query returned no rows
How to Fix It
-
Use isset() to check if the key exists before reading it. Example: if (isset($_POST['email'])) { $email = $_POST['email']; } else { $email = ''; }
isset() returns false if the key does not exist OR if the value is null. It is the most common check for form inputs.
-
Use the null coalescing operator (??) as a concise shorthand (PHP 7+). Example: $email = $_POST['email'] ?? ''; — this returns the value if the key exists, or an empty string if it does not.
The ?? operator is much more readable than the equivalent isset() ternary. Use it throughout your PHP 7+ code for default values.
-
Use array_key_exists() when you need to distinguish between a missing key and a key with a null value. isset() treats null values as non-existent; array_key_exists() does not.
Example: if (array_key_exists('discount', $order)) { applyDiscount($order['discount']); } — this fires even if $order['discount'] is null.
-
For superglobal arrays ($_POST, $_GET, $_REQUEST), use filter_input() which safely returns null for missing keys without generating a notice. Example: $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
filter_input() also sanitizes the value in one step, which is a security best practice for user input.
-
In PHP 8, consider using error reporting settings for development vs production. Set error_reporting(E_ALL) in development to catch all notices, and set display_errors to Off in production (log them instead).
Undefined index in PHP 8 is classified as a Warning (higher severity than PHP 7's Notice) but is still non-fatal.
When to Call a Professional
Undefined index is a notice-level PHP error that you resolve in your own code. No server administrator or external service is needed. In PHP 8.0 and later, the equivalent message is 'Undefined array key' and has the same fix.
Frequently Asked Questions
What is the difference between Undefined index and Undefined variable in PHP?
Undefined variable means you used a variable ($name) that was never declared or assigned a value. Undefined index means the variable exists and is an array, but the specific key you requested (['email']) does not exist in it. Both are notice/warning-level errors in PHP 7 and 8. Both are fixed with isset() checks — for variables, check isset($myVar); for array keys, check isset($array['key']).
Is Undefined index a security issue?
Not directly — it does not expose your code to attack by itself. However, code that does not check for undefined indices often also lacks other input validation. If you are reading $_GET or $_POST without isset() checks, you may also be failing to sanitize the values you do receive. Fix undefined index notices and add input validation and sanitization at the same time.
How do I suppress Undefined index notices without fixing them?
You can use the error suppression operator @ before the array access: $val = @$_POST['key']; — but this is strongly discouraged. @ suppresses the notice but does not fix the bug, and it silences all errors in that expression including serious ones. The correct approach is to add proper isset() checks. In PHP 8, suppressing undefined key warnings with @ is considered especially bad practice.