为什么我不能在函数的返回值上使用数组索引?
为什么我不能这样做?
explode(',','1,2,3', 1)[0]
所有其他语言都支持它。
我正在寻找的答案:(因为人们似乎认为这是毫无意义的咆哮)
我应该意识到变量和返回值的行为方式之间是否存在某种差异?
这是一个设计决定吗?他们只是懒惰吗?该语言的构建方式是否存在某种原因导致其实现起来极其困难?
Why can't I do this?
explode(',','1,2,3', 1)[0]
Every other language supports it.
Answers I'm looking for: (since people seem to think this is a pointless rant)
Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of?
Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为PHP目前不支持这种语法。但你可以将它传递给一个函数,例如:
否则,你必须先将返回值分配给一个变量,然后通过索引访问。
更多信息:
http:// /schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html
所以,它正在开发中,但正如我所说,目前还没有支持。
更新
PHP >= 5.4 现在支持函数数组解引用,例如
foo()[0]
Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.:
Otherwise, you have to assign the return value to a variable first, then access via index.
More Info:
http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html
So, it is in the development pipeline but, as I stated, is not currently supported.
Update
PHP >= 5.4 now supports function array dereferencing, e.g.
foo()[0]
在我的框架中,我编写了一个函数,能够对所有可能的数组执行此操作:
In my framework I wrote a function to be able to do it with all possiible array:
爆炸返回一个数组,而不是引用。
Explode return an array, not reference.
您也可以考虑......
或
You might also consider....
or
使用第一个索引:
Use the first index with: