|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50617 PHP should have Perl style obtaining of values from arrays returned from functi
Submitted: 2009-12-31 05:16 UTC Modified: 2009-12-31 06:24 UTC
Avg. Score:2.8 ± 1.8
Reproduced:2 of 4 (50.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: billyellison99 at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.2RC1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: billyellison99 at gmail dot com
New email:
PHP Version: OS:


 [2009-12-31 05:16 UTC] billyellison99 at gmail dot com
In Perl, I am able to do something like:
print (explode(",", $myString))[1];

which would print the second result of explode (which returns an 
array). Sadly, this feature is lacking in PHP. I'd really like to see 
this in PHP, because it adds so much convenience to coding, and 
shortens it, rather than having to assign to a variable, then obtaining 
the value from the variable.

Reproduce code:
$myString = "The,quick,brown,fox";
$secondpart = (explode(",", $myString))[1];
echo $secondpart;

Expected result:

Actual result:
Parse error: syntax error, unexpected '[' in 
/home/billy/Desktop/index.php on line 3


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-31 05:59 UTC] jrhodes at roket-enterprises dot com
I second this feature request.

Function Array Dereferencing allows programmers to write cleaner code 
and is adopted by many other programming languages such as Ruby and 
Python.  Adding this feature doesn't break any previous code written 
in PHP.

With FAD:
$var = func()[2];

Without FAD: (Current PHP)
$var = func();
$var = $var[2];
$var = func();
$othervar = $var[2];

With FAD:

Without FAD:
$var = func();
 [2009-12-31 06:24 UTC]
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

This is a duplicate of
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu May 19 09:03:49 2022 UTC