Windows上的语法错误,而不是Linux上的语法错误

The following code is giving me errors when run on windows with php version 5.3.5:

$dir_widgets = '/var/www/site-sp/dir1/filename.json';

error here:

$folder_name = array_reverse( explode( '/' , $dir_widgets ) )[ 1 ];

This is the error reported:

*Parse error*: syntax error, unexpected '[' in 

On GNU/Ubuntu 13.04 with PHP version 5.4.9 it works fine.

Can be the PHP version? Is it bad practice to do the following?

array_reverse( explode( '/' , $dir_widgets ) )[ 1 ]

Thanks.

Accessing an array index directly from a return value was added in PHP 5.4 hence why you are getting a syntax error with version 5.3. You'll need to save the return value from the function to a temporary variable and then access the index you want.

Something like:

$tmp_array = array_reverse( explode( '/' , $dir_widgets ) );
$tmp_array[1] ...

PHP 5.3 won't let you reference a value in an array as you are trying to do.

You need to set the return from array_reverse() to a variable and the reference the element from that variable a la $var = array_reverse(); $var[1];