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'];

?>

Solutions

  • 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.