"Undefined array key" in PHP — What It Means and How to Fix It
What the PHP error "Undefined array key" means and how to fix it.
Published October 31, 2026
This warning means your code read an array element whose key does not exist. In PHP 8 reading a missing key returns null and emits a warning.
What it means and why it happens
Common with $_POST, $_GET and JSON data where fields may be absent, or with typos in key names and off-by-one numeric indexes.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Warning: Undefined array key "email" in form.php on line 12
Practical examples
Code that triggers it
<?php
$email = $_POST['email']; // key may not existThe fix
<?php
$email = $_POST['email'] ?? null;
// or
if (array_key_exists('email', $_POST)) { $email = $_POST['email']; }Validate input
$data = filter_input_array(INPUT_POST, ['email' => FILTER_VALIDATE_EMAIL]);
if (empty($data['email'])) { http_response_code(422); exit('Email required'); }Common mistakes
- Trusting that form fields always exist.
- Using isset on a key whose value can be null when you need array_key_exists.
- Numeric off-by-one indexes.
Key takeaways
- Undefined array key: This warning means your code read an array element whose key does not exist.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
isset vs array_key_exists?
isset is false for null values; array_key_exists is true if the key exists, even with null.
How do I find which line caused Undefined array key?
The error message and stack trace show the file and line; start there and work backwards.