开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜