-3

Let's suppose I have a function that returns an array. Let's name it arrFunc().
Now, if I need a quick use of this function (eg. return first/second/... value of the array), can I write something like this arrFunc()[0] to return first array value? Or, is there something similar?

This is just a personal curiosity...

Xriuk
  • 382
  • 2
  • 7
  • 26
  • 4
    `arrFunc()[0]` works in PHP 5.4+. – gen_Eric Apr 24 '14 at 17:07
  • @RocketHazmat Does it work even with associative arrays? Like `arrFunc()["test"]`. – Xriuk Apr 24 '14 at 17:11
  • 3
    If you have an idea of how to do something, you should really try it first, before asking if it works. – Patrick Q Apr 24 '14 at 17:11
  • 2
    This would have been really easy to test yourself. If you had tried on a pre-5.4 version, got an error, and were looking for an alternative, that would be a valid question. But it seems you haven't done so, so this is just plain lazy. – IMSoP Apr 24 '14 at 17:12
  • 2
    Meanwhile, here is a site where you can test the same PHP code in multiple versions: http://3v4l.org/ – IMSoP Apr 24 '14 at 17:12
  • 1
    @Xriuk: Yeah, it does. – gen_Eric Apr 24 '14 at 17:18
  • possible duplicate of [Access array returned by a function in php](http://stackoverflow.com/questions/1459377/access-array-returned-by-a-function-in-php) – Patrick Q Apr 24 '14 at 17:25

1 Answers1

3

Yes, you can :

function arr()
{
    return array(1, 2, 3, 4);
}

echo arr()[0];

give this result:

1

Pay attention that it's only true for PHP 5.4+ !

Emrys Myrooin
  • 2,179
  • 14
  • 39