如何用三元运营商编写此条件陈述
我想检查用户的 usr_name
是否为空,然后获取他的电子邮件并调整新变量。
因此,这是传统的方式:
if(auth()->user()->usr_name != null){
$user_input = auth()->user()->usr_name;
}else{
$user_input = auth()->user()->usr_email;
}
现在我想用三元条件运算符编写此内容,所以我尝试了以下方式:
$ user_input = empty(auth(auth() - > user() - > usr_name)? auth() - > user() - > usr_name:auth() - > user() - > usr_email;
代码> $ user_input 。
那么,与三元运营商编写此书的正确方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
php中的简短语法。 The above code is the same as
Which is most likely equivalent to your code, considering the non strict
!= null
check.Ternary operator has a short syntax in PHP. The above code is the same as
Which is most likely equivalent to your code, considering the non strict
!= null
check.租赁操作员在“?”之前检查结果如果True返回第一对,则以“:”返回第二对。
here C equals to 1
In your example you must changed order of tenary result values
Tenary operator check the result before "?" and if true returns first pair distinguished with ":" if not return second pair.
here C equals to 1
In your example you must changed order of tenary result values
您只需将逻辑回到正面
,因此可以清楚地表明,如果设置了usr_name,则使用usr_email
请注意,您可以将其放入登录器中,然后将其称为
auth() - &gt之类的东西; user() - >标识符
项目中的任何地方You just have your logic back to front
So to be clear, you are giving priority to usr_name if it is set, otherwise use the usr_email
Note that you could put this in an accessor and then call something like
auth()->user()->identifier
anywhere in your project如果您使用php> = 7.0,则可以使用零粉化操作员编写一个非常漂亮的语句。
看起来像:
If you use PHP >= 7.0 you could use the null-coalescing operator to write a really beautiful statement instead.
It would look something like: