如何用三元运营商编写此条件陈述

发布于 2025-01-21 22:49:36 字数 517 浏览 4 评论 0 原文

我想检查用户的 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 。

那么,与三元运营商编写此书的正确方法是什么?

I want to check if the usr_name of user is empty, then get his email and adjust a new variable to it.

So here is the traditional way:

if(auth()->user()->usr_name != null){
    $user_input = auth()->user()->usr_name;
}else{
    $user_input = auth()->user()->usr_email;
}

Now I want to write this with ternary condition operators, so I tried this:

$user_input = empty(auth()->user()->usr_name) ? auth()->user()->usr_name : auth()->user()->usr_email;

But this is wrong, since it returns null for $user_input.

So what is the correct way of writing this with ternary operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2025-01-28 22:49:36
$user_input = auth()->user()->usr_name ?: auth()->user()->usr_email;

php中的简短语法。 The above code is the same as

if (auth()->user()->usr_name) {
    $user_input = auth()->user()->usr_name;
} else {
    $user_input = auth()->user()->usr_email;
}

Which is most likely equivalent to your code, considering the non strict != null check.

$user_input = auth()->user()->usr_name ?: auth()->user()->usr_email;

Ternary operator has a short syntax in PHP. The above code is the same as

if (auth()->user()->usr_name) {
    $user_input = auth()->user()->usr_name;
} else {
    $user_input = auth()->user()->usr_email;
}

Which is most likely equivalent to your code, considering the non strict != null check.

[旋木] 2025-01-28 22:49:36

租赁操作员在“?”之前检查结果如果True返回第一对,则以“:”返回第二对。

Let say A = true 
C = A ? 1: 2 ; 

here C equals to 1

In your example you must changed order of tenary result values

$user_input = empty(auth()->user()->usr_name) ?auth()->user()->usr_email : auth()->user()->usr_name 

Tenary operator check the result before "?" and if true returns first pair distinguished with ":" if not return second pair.

Let say A = true 
C = A ? 1: 2 ; 

here C equals to 1

In your example you must changed order of tenary result values

$user_input = empty(auth()->user()->usr_name) ?auth()->user()->usr_email : auth()->user()->usr_name 
东走西顾 2025-01-28 22:49:36

您只需将逻辑回到正面

$user_input = empty(auth()->user()->usr_name) ? auth()->user()->usr_email : auth()->user()->usr_name;

,因此可以清楚地表明,如果设置了usr_name,则使用usr_email

请注意,您可以将其放入登录器中,然后将其称为 auth() - &gt之类的东西; user() - >标识符项目中的任何地方

You just have your logic back to front

$user_input = empty(auth()->user()->usr_name) ? auth()->user()->usr_email : auth()->user()->usr_name;

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

柒七 2025-01-28 22:49:36

如果您使用php> = 7.0,则可以使用零粉化操作员编写一个非常漂亮的语句。

看起来像:

$user_input = auth()->user()->usr_name ?? auth()->user()->usr_email;

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:

$user_input = auth()->user()->usr_name ?? auth()->user()->usr_email;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文