PHP Warning: Undefined array key - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

PHP Warning: Undefined array key
================================

### [\#PHP](https://rocketeersapp.com/knowledge/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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [About the error](#content-about-the-error)
2. [Why do I see this error](#content-why-do-i-see-this-error)
3. [Solution](#content-solution)
4. [Provide a default with the null coalescing operator](#content-provide-a-default-with-the-null-coalescing-operator)
5. [Check existence explicitly](#content-check-existence-explicitly)
6. [In Laravel](#content-in-laravel)

[\#](#content-about-the-error "Permalink")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.

[\#](#content-why-do-i-see-this-error "Permalink")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 `null` rather than an array, so there's no key to read.
- Code that "worked" on PHP 7 because the notice was hidden.

[\#](#content-solution "Permalink")Solution
-------------------------------------------

### [\#](#content-provide-a-default-with-the-null-coalescing-operator "Permalink")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;

```

### [\#](#content-check-existence-explicitly "Permalink")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.

### [\#](#content-in-laravel "Permalink")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](/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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#PHP](https://rocketeersapp.com/knowledge/php)

- [How to run PHP files](https://rocketeersapp.com/knowledge/run-php-files)
- [PHP file upload exceeds upload\_max\_filesize](https://rocketeersapp.com/knowledge/php-file-upload-exceeds-upload-max-filesize)
- [PHP Fatal error: Allowed memory size exhausted](https://rocketeersapp.com/knowledge/php-allowed-memory-size-exhausted)
- [PHP: Call to a member function on null](https://rocketeersapp.com/knowledge/call-to-a-member-function-on-null)
- [PHP Fatal error: Class "X" not found](https://rocketeersapp.com/knowledge/php-class-not-found)
- [Composer out of memory (allowed memory size exhausted)](https://rocketeersapp.com/knowledge/composer-out-of-memory-allowed-memory-size-exhausted)

 [View all 11 articles →](https://rocketeersapp.com/knowledge/php)
