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;
精彩评论