PHP syntax for dereferencing function result

后端 未结 22 1130
不知归路
不知归路 2020-11-22 02:14

Background

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring

22条回答
  •  轮回少年
    2020-11-22 02:49

    Array Dereferencing is possible as of PHP 5.4:

    • http://svn.php.net/viewvc?view=revision&revision=300266

    Example (source):

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

    with PHP 5.3 you'd get

    Parse error: syntax error, unexpected '[', expecting ',' or ';' 
    

    Original Answer:

    This has been been asked already before. The answer is no. It is not possible.

    To quote Andi Gutmans on this topic:

    This is a well known feature request but won't be supported in PHP 5.0. I can't tell you if it'll ever be supported. It requires some research and a lot of thought.

    You can also find this request a number of times in the PHP Bugtracker. For technical details, I suggest you check the official RFC and/or ask on PHP Internals.

提交回复
热议问题