php - Unexpected '[' on return explode(" ", $message)[0]; -


in php, i'm getting "parse error: syntax error, unexpected '['" on following line of code:

return explode(" ", $message)[0]; 

i'd give more details, that's it. line looks unimpeachable me, , in fact running fine until copied file. biggest difference line part of class, , it's being called big software library don't know about. causing issue?

a bit more context:

public function getirccommand($message) {     if ($message[0] != ":")     {         return explode(" ", $message)[0];     }     else     {         return preg_split("/\s+/", $message)[1];     } } 

you're running on php 5.3 or lower. need upgrade @ least php 5.4 working (or use temporary variable access elements of function returning array).

what you're doing there called array dereferencing. manual says:

as of php 5.4 possible array dereference result of function or method call directly. before possible using temporary variable.

as of php 5.5 possible array dereference array literal.

and, check that, let's try run

<?php echo explode(" ", "1 2 3")[1]; 

for kinds of diferent php versions. results are, php versions 5.4 , hihgher output "2", desired result.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -