简体   繁体   中英

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.

addendum : There was a vote to allow passing arbitrary expression arguments to isset and empty , but finally isset was dropped from the final implementation.

isset only and exclusively works on variables. You do not need it for functions. Your ternary operator is fine.

See The Definitive Guide To PHP's isset And empty .

try: "

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

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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