简体   繁体   English

一定有更好的方法(php,string)

[英]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? 下面的代码功能,但我不喜欢它如何需要这么多行,我隐约回想起一些可能用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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