PHP syntax for dereferencing function result

后端 未结 22 862
不知归路
不知归路 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:59

    my usual workaround is to have a generic function like this

     function e($a, $key, $def = null) { return isset($a[$key]) ? $a[$key] : $def; }
    

    and then

      echo e(someFunc(), 'key');
    

    as a bonus, this also avoids 'undefined index' warning when you don't need it.

    As to reasons why foo()[x] doesn't work, the answer is quite impolite and isn't going to be published here. ;)

提交回复
热议问题