Mike Atlas Mike Atlas - 1 year ago 61
PHP Question

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

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


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download