David Morrow
David Morrow

Reputation: 9354

php access array value from function return

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

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

instead you have to do this

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

Upvotes: 9

Views: 770

Answers (3)

Anthony Forloney
Anthony Forloney

Reputation: 91796

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

Upvotes: 5

Kendall Hopkins
Kendall Hopkins

Reputation: 44104

Poor language/interpreter design.

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

Upvotes: 1

Amy B
Amy B

Reputation: 17977

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.

Upvotes: 5

Related Questions