简体   繁体   中英

There must be a better way (php, string)

I want to display a user's firstname if they gave us one, otherwise their email. The code below functions, but I don't like how it takes so many lines, I vaguely recall some way of maybe doing this with OR? I have (essentially):

if ($firstname == "")
{
$name   = $email;
}else{
$name = $firstname;
}
echo $name;
echo ($firstname) ? $firstname : $email;

较短的版本(PHP 5)

echo ($firstname ? : $email);

这是一个不常见但有时有用的构造(作为?:语法的替代):

 $name = $firstname   OR   $name = $email;

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