简体   繁体   English

三元运算符 - 我做错了什么?

[英]Ternary Operator - What am I doing wrong?

$profilePic = isset( $userservice->getProfilePic($userid))      
              ?  filter($userservice->getProfilePic($userid)) 
              : '<img></img>';

Returns the error: 返回错误:

Fatal error: Can't use method return value in write context

What am I doing wrong here? 我在这做错了什么?

You can only use isset with variables, not with return values from functions, as isset is not a function, but a language construct. 您只能将isset与变量一起使用,而不能使用函数的返回值,因为isset不是函数,而是语言构造。

addendum : There was a vote to allow passing arbitrary expression arguments to isset and empty , but finally isset was dropped from the final implementation. 附录 :有一个投票允许将任意表达式参数传递给issetempty ,但最终isset从最终实现中删除。

isset only and exclusively works on variables. 仅限isset并且专门用于变量。 You do not need it for functions. 你不需要它来实现功能。 Your ternary operator is fine. 你的三元运算符很好。

See The Definitive Guide To PHP's isset And empty . 请参阅PHP的权威指南isset并为空

try: " 试试:“

$tempPic=$userservice->getProfilePic($userid);
         $profilePic = isset($tempPic ) ?  filter($userservicegetProfilePic($userid))  : '<img></img>';

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM