Mike Atlas Mike Atlas - 1 month ago 9
PHP Question

Why can't I access the exploded array element immediately?

Why can't I immediately access elements in the array returned by

explode()
?

For example, this doesn't work:

$username = explode('.',$thread_user)[1];
//Parse error: syntax error, unexpected '[


But this code does:

$username = explode('.',$thread_user);
$username = $username[1];


I don't usually program in PHP, so this is rather confusing to me.

Answer

Actually, PHP simply does not support this syntax. In languages like Javascript (for instance), the parser can handle more complex nesting/chaining operations, but PHP is not one of those languages.

Comments