Index invalid or out of range

Description

This error message is returned by the SplFixedArray object, when trying to access indices outside the validity interval. A SplFixedArray has a limited number of elements, set at the beginiing, or updated laster with setSize().

On a regular array, accessing an undefined index is a warning, not a fatal error.

A different message is emited when the index is not an integer.

Example

<?php

$object = new SplFixedArray(3);

$object[1] = 3;
echo $object[1]; // OK
echo $object[0]; // OK, returns null

echo $object[-1];
//Fatal error: Uncaught RuntimeException: Index invalid or out of range

?>

Solutions

  • Check the size of the array before accessing one of the index.

  • Check that the index is zero or more.

  • Use a regular array.