Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Undefined array key

Description

This notice is emitted when an offset (string or integer) is being used before being defined. In a word, the element at this offset does not exist, so PHP creates it on the fly, as NULL, then uses it.

Example

<?php

// undefined array key 4
echo [1,2,3][4];

// undefined array key 'a'
echo [1,2,3]['a'];

// undefined array key 1
list($a, $b) = ['c'];

?>

Alternatives

  • Test the existence of the offset before usage.
  • Gives the offset a default value before usage.
  • Check that the right operand of a list() call has enough elements to fit the left operand.

In previous PHP versions, this error message used to be :ref:undefined-offset.