php.net |  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
Votes:4
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
35 - 7 = ?
Subscribe to this entry?

 
 [2009-12-31 05:16 UTC] billyellison99 at gmail dot com
Description:
------------
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:
---------------
<?php
$myString = "The,quick,brown,fox";
$secondpart = (explode(",", $myString))[1];
echo $secondpart;
?>

Expected result:
----------------
quick

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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.

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

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

With FAD:
----
otherfunc(func()[2]);

Without FAD:
----
$var = func();
otherfunc($var[2]);
unset($var);
 [2009-12-31 06:24 UTC] joey@php.net
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 http://bugs.php.net/bug.php?id=23022
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed May 25 22:04:07 2022 UTC