Error guides

Undefined array key

You read an array key that is not set. PHP 8 warns and Laravel throws an ErrorException. Read it with ??, Arr::get or validated input.

Message
Undefined array key "email"
Exception
ErrorException

What it means

You read $data['email'], and the array has no email key. Since PHP 8.0 that gives the warning Undefined array key "email", or Undefined array key 3 for a number, and the expression becomes null. PHP 7 and older gave a notice instead, worded Undefined index: email or Undefined offset: 3. Laravel turns every warning into an ErrorException, so in a Laravel app the request stops on that line.

Why it happens

  • Optional input read as if it were always there. A form or API client that leaves out a field makes $request->all()['email'] or $_POST['email'] fail.
  • A decoded JSON payload without the field. json_decode($body, true)['email'] assumes the sender always includes it.
  • An index one past the end. A list of three items has the keys 0, 1 and 2, so $items[count($items)] reads key 3.
  • A renamed key. Code still reads user_email after the API, the config file or a migration renamed it to email.

How to fix it

When a missing key is normal, give it a default with ??. It does not raise a warning:

<?php

$email = $data['email'] ?? null;

To test for a key, know how isset and array_key_exists differ. isset returns false for a key that exists with the value null; array_key_exists returns true:

<?php

$data = ['email' => null];

var_dump(isset($data['email']));             // bool(false)
var_dump(array_key_exists('email', $data));   // bool(true)
var_dump($data['email'] ?? 'none');           // string(4) "none"

In Laravel, Arr::get and data_get read nested keys with dot notation and take a default as the third argument. data_get works on objects too. For request input, input takes a default as the second argument:

<?php

use Illuminate\Support\Arr;

$zip = Arr::get($data, 'address.zip', 'unknown');
$city = data_get($payload, 'customer.address.city');
$email = $request->input('email', 'none@example.com');

When the key must be there, validate the input before you use it. Every field you marked required exists in the result, and a request without it gets a validation error response instead of a 500. An optional field the request left out is missing from the result, so read it with ??:

<?php

$validated = $request->validate([
    'email' => ['required', 'email'],
    'phone' => ['nullable', 'string'],
]);

$email = $validated['email'];
$phone = $validated['phone'] ?? null;

For an index past the end, loop with foreach, or read the last item with array_last($items) (PHP 8.5) or Arr::last($items). For a renamed key, search the code for the old name.

Find it in production

In Laravel with APP_DEBUG=false, a visitor gets a 500 "Server Error" page and storage/logs/laravel.log has ErrorException: Undefined array key "email" with a stack trace that points at the line. Because Laravel reports it, Ranetrace error monitoring records it with the stack trace and the request it happened on.

Plain PHP is quieter. The warning does not stop the script, so with display_errors off the page returns 200 with an empty value where the key should have been, and the warning only reaches the PHP error log. Wrong data can travel on for a long time that way before anything fails.

Reproduced on PHP 8.5, Laravel 13.45, 5 December 2026.

Start monitoring today

Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.

Start your 30-day free trial From $9 a month. No credit card required.