4

I'm writing a small repository for my little app team's Java code, and I have this error all over my code.

$base = explode(".", $class)[0];

The problem occurs only with this one line of code, every time. As far as I know, the above is correct PHP syntax, so what's going on?

Parse error: syntax error, unexpected '[' in .../mitc/code/index.php on line 27

If you'd like to see the error, it's at http://chancehenrik.x10.mx/mitc/code/ and elsewhere on my site.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Sammi De Guzman
  • 567
  • 9
  • 20

3 Answers3

13

That's called array dereferencing and only works in PHP 5.4+. You're probably running PHP 5.3.x wherever you are getting that error.

See results based on different PHP versions

John Conde
  • 217,595
  • 99
  • 455
  • 496
0
$exploded = explode(".", $class);
$base = $exploded[0];
Musa
  • 96,336
  • 17
  • 118
  • 137
Antony
  • 14,900
  • 10
  • 46
  • 74
0

To work on older versions of PHP (<5.4), you should do:

list($base) = explode(".", $class);

That is:

list($a, $b, $c) = array(1, 2, 3);

Now $a=1, $b=2, and $c=3.

Matthew
  • 47,584
  • 11
  • 86
  • 98