5

Why can't I do this?

explode(',','1,2,3', 1)[0]

Every other language supports it.

Answers I'm looking for: (since people seem to think this is a pointless rant)

Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of?

Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement?

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    Because it's not supported by the language parser. PHP 5.4 might add it. -- Until then, there are [a few fiddly workarounds](http://stackoverflow.com/questions/5491885/shortcut-for-foo-explode-bla-ble-bli-echo-foo0/5491911#5491911). – mario Jun 02 '11 at 15:59
  • 6
    "Every other language", eh? Anyway, this "question" really just amounts to a rant. You know it's not possible, so there's no useful answer to this question. – Lightness Races in Orbit Jun 02 '11 at 16:00
  • 2
    @Tomalak: Not really. There could be a fundamental misunderstanding I have about PHP and how it works. For example, it could be returning something other than what I expect; there could be some difference between how a variable behaves and how a return value behaves. – mpen Jun 02 '11 at 16:04
  • 2
    it does not seem so badly a rant imo. This could be a nice feature if it will get implemented in a newer version of PHP. Same for `(new Obj())->method();` – dynamic Jun 02 '11 at 16:10
  • 1
    I would add a 'design' tag to this question, since it is as much about the design of programming languages as it is about php. There is probably some design decision that led the authors to not include this feature, and that is what the question is asking for. Fair question. – Michael Venable Jun 02 '11 at 16:22
  • @Mark: You have since improved the question, which was going to be my next suggestion. – Lightness Races in Orbit Jun 02 '11 at 16:28
  • 2
    This question was edited to provide clarification so earlier close votes may no longer be warranted (including mine). I'm voting to reopen. – webbiedave Jun 02 '11 at 17:12

5 Answers5

7

Why can't I do this?

Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.:

current(explode(',','1,2,3', 1));

Otherwise, you have to assign the return value to a variable first, then access via index.

More Info:

So one can chain method calls or property access. Now for a long time people requested the same thing for array offset access. This was often rejected due to uncertainties about memory issues, as we don't like memory leaks. But after proper evaluation Felipe committed the patch which allows you to do things like:

function foo() {
    return array(1, 2, 3);
}
echo foo()[2]; // prints 3

http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

So, it is in the development pipeline but, as I stated, is not currently supported.

Update

PHP >= 5.4 now supports function array dereferencing, e.g. foo()[0]

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • Explain the downvote. The answer is correct: "PHP doesn't currently support this syntax." – webbiedave Jun 02 '11 at 16:07
  • I didn't downvote you, but... "doesn't support it" wasn't really the answer I was looking for. *Why?* Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceeding difficult to implement? – mpen Jun 02 '11 at 16:12
  • Very well. I'll add more info. – webbiedave Jun 02 '11 at 16:13
  • Just do http://stackoverflow.com/a/13113/632951 – Pacerier Dec 09 '14 at 11:45
2

In my framework I wrote a function to be able to do it with all possiible array:

function elem($array,$key) {
  return $array[$key];
}

//> usage 
echo elem($whateverArrayHere,'key');
echo elem(explode(),1);
dynamic
  • 46,985
  • 55
  • 154
  • 231
1

Explode return an array, not reference.

$tab = explode (',','1,2,3', 1);
$tab[0] = ','
red eyes dev
  • 398
  • 3
  • 11
0

You might also consider....

  list($value_I_want_to_keep)=explode(',','1,2,3', 1);

or

  $value_I_want_to_keep=strtok('1,2,3',',');
symcbean
  • 47,736
  • 6
  • 59
  • 94
0
$pieces = explode(',','1,2,3', 1);

Use the first index with:

$pieces[0];
gpresland
  • 1,690
  • 2
  • 20
  • 31