开发者

There must be a better way (php, string)

开发者 https://www.devze.com 2023-03-08 01:10 出处:网络
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

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 (essential开发者_JAVA技巧ly):

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


echo ($firstname) ? $firstname : $email;


The shorter version (PHP 5)

echo ($firstname ? : $email);


This is a less common but sometimes useful construct (as alternative to the ?: syntax):

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

精彩评论

暂无评论...
验证码 换一张
取 消