php access array value from function return

落花浮王杯 提交于 2019-12-10 03:36:16

问题


silly php question... why cant i do this?

echo Auth::getFullUser()[ 'country' ];

instead you have to do this

$user = Auth::getFullUser();
echo $user[ 'country' ];

回答1:


The syntax just doesn't allow it unfortunately.

AFAIK there was at one time intention to put that syntax in PHP6, but it has been dropped.




回答2:


PHP grammar only allows subscript notation (i.e. ['country']) on the end of a variable expression (i.e. $user) not an expression (i.e. Auth::getFullUser())




回答3:


Poor language/interpreter design.

Same reason you can't do "functionname"() and functions are case insensitive.



来源:https://stackoverflow.com/questions/2875163/php-access-array-value-from-function-return

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!