Unexpected brute '[' - PHP

I am writing a small repository for my little Java application command code and I have this error all over my code.

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

      

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

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

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

+3


source to share


3 answers


This is called array dereferencing and only works in PHP 5.4+ . You are probably using PHP 5.3.x wherever you get this error.



View results based on different PHP versions

+12


source


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

      



0


source


To work with older PHP versions (<5.4), you must:

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

      

I.e:

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

      

Now $a=1

, $b=2

and $c=3

.

0


source







All Articles