-1

Why does this work:

$n = explode("@", "some@email.com");
echo $n[0];

And this not work?

explode("@", "some@email.com")[0]

When I try the latter, I get:

Parse error: syntax error, unexpected '['
ddavison
  • 25,442
  • 12
  • 70
  • 96

1 Answers1

1

It works in later versions of PHP (>= 5.4.0):

PHP 5.4.0 offers a wide range of new features:

[...] - Function array dereferencing has been added, e.g. foo()[0]. [...]

Older versions of PHP do not support function array dereferencing, which is why you get a syntax error (PHP does not know what to do with the [, so it tells you it is "unexpected").

Community
  • 1
  • 1
Sverri M. Olsen
  • 12,362
  • 3
  • 32
  • 50
  • Link to some examples: https://wiki.php.net/rfc/functionarraydereferencing – Mike W May 29 '14 at 22:40
  • i'm using 5.3.6... that would explain it. Thanks so much! It was breaking the [principle of least astonishment](http://en.wikipedia.org/wiki/Principle_of_least_astonishment) `:)` – ddavison May 29 '14 at 22:40