ID:          50617
 Comment by:  jrhodes at roket-enterprises dot com
 Reported By: billyellison99 at gmail dot com
 Status:      Open
 Bug Type:    Feature/Change Request
 PHP Version: 5.3.2RC1
 New Comment:

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);


Previous Comments:
------------------------------------------------------------------------

[2009-12-31 05:16:57] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50617&edit=1

Reply via email to