Knowledge
PHP Warning: Undefined array key
#PHP
PHP 8 turned accessing a missing array key into a warning instead of a silent notice. The fix is to check the key exists or provide a default before reading it.
Published by Mark van Eijk on June 23, 2026 · 1 minute read
- About the error
- Why do I see this error
- Solution
- Provide a default with the null coalescing operator
- Check existence explicitly
- In Laravel
About the error
PHP Warning: Undefined array key "email" in /var/www/app.php on line 12
You read $array['email'] but that key doesn't exist. In PHP 7 this was a quiet E_NOTICE many people never saw; PHP 8 promoted it to an E_WARNING, so upgrading a codebase surfaces a flood of these. A closely related message is Trying to access array offset on value of type null, which means the thing you indexed wasn't an array at all.
Why do I see this error
- The key really isn't there (an optional form field, a missing query parameter).
- A typo in the key name.
- The variable is
nullrather than an array, so there's no key to read. - Code that "worked" on PHP 7 because the notice was hidden.
Solution
Provide a default with the null coalescing operator
The cleanest fix is ??, which returns the right-hand side when the key is missing or null:
$email = $_POST['email'] ?? null;
$page = $_GET['page'] ?? 1;
Check existence explicitly
When you need to branch on whether the key is present:
if (array_key_exists('email', $data)) {
// present, even if its value is null
}
if (isset($data['email'])) {
// present and not null
}
Note the difference: isset() treats a key with a null value as "not set", while array_key_exists() only checks the key is there.
In Laravel
Use data_get() or request helpers, which return null (or a default) instead of warning:
$value = data_get($array, 'user.email', 'unknown');
$page = $request->input('page', 1);
If the underlying value is an object rather than an array and you're calling a method on it, see Call to a member function on null.
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!
Related articles
How to run PHP files
PHP 8 turned accessing a missing array key into a warning instead of a silent notice. The fix is to check the key exists or provide a default before reading it.
PHP Fatal error: Class "X" not found
PHP 8 turned accessing a missing array key into a warning instead of a silent notice. The fix is to check the key exists or provide a default before reading it.