PHP – Array dereferencing

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly.
Before it was only possible using a temporary variable.

As of PHP 5.5 it is possible to array dereference an array literal.

<?php
function getArray() {
    return array(1, 2, 3, 4);
}

// on PHP 5.4
$secondElement = getArray()[1];

// earlier
$temp = getArray();
$secondElement = $temp[1];
?>

Leave a Reply