php access array value from function return

2019-04-23 14:06发布

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

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

instead you have to do this

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

标签: php arrays scope
3条回答
太酷不给撩
2楼-- · 2019-04-23 14:21

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

查看更多
Viruses.
3楼-- · 2019-04-23 14:28

Poor language/interpreter design.

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

查看更多
仙女界的扛把子
4楼-- · 2019-04-23 14:31

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.

查看更多
登录 后发表回答