2

This code fails on some machines:

// Parse error: syntax error, unexpected '[' ...
return json_encode(self::GenerateSomeAssociativeArray($meta_file)["list"]);

but this effectively identical version works on all machines:

$foo = self::GenerateSomeAssociativeArray($meta_file);
return json_encode($foo['list']);

I assume it's a versioning issue, but I'm unable to find information on the difference, probably because I'm unfamiliar with the terminology to search.

ty.
  • 10,924
  • 9
  • 52
  • 71
  • You'll find it in this list of syntax/feature additions: http://www.php.net/manual/en/migration54.new-features.php – mario Jun 14 '12 at 18:44

2 Answers2

3

The first example you gave is for PHP 5.4 and supposedly higher. See the following PHP 5.4 release announcement:

http://php.net/releases/5_4_0.php

<?php
// Example #8 Array dereferencing

function getArray() {
    return array(1, 2, 3);
}

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

// previously
$tmp = getArray();
$secondElement = $tmp[1];

// or
list(, $secondElement) = getArray();
?>
Rick Kuipers
  • 6,616
  • 2
  • 17
  • 37
2

Versions of PHP prior to 5.4 don't allow the array indexing operator ([]) to be applied to anything other than a variable.

Yes, really.