php: $a=$b OR $a=$c 与三元

发布于 2024-11-15 14:17:29 字数 413 浏览 3 评论 0原文

我需要将两个变量之一分配给第三个变量,如果第一个变量为 (bool)false 或未定义,则使用第二个变量的值。

我通常使用三元表示法来执行此操作,如下所示:

$foobar = ($some_prefix_and_some_variable_name) ? $some_prefix_and_some_variable_name : $bar ; 

但有时,如果 $foo 变量名称很长,则这不是那么漂亮,因为它需要在此表示法中重复。

我现在的问题是,使用这种表示法是否同样好:

$foobar = $some_prefix_and_some_variable_name OR $foobar = $bar;

这种表示法可以与三元版本互换吗?

I need to assign one of two variables to a third variable, using the value of the second variable if the first is (bool)false or undefined.

I usually do this using ternary notation like so:

$foobar = ($some_prefix_and_some_variable_name) ? $some_prefix_and_some_variable_name : $bar ; 

but sometimes this is not so pretty if the $foo variable name is very long, as it needs to be repeated in this notation.

My question is now, is it just as good to use this notation:

$foobar = $some_prefix_and_some_variable_name OR $foobar = $bar;

and is this notation interchangeable with the ternary version?

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

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

发布评论

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

评论(5

嘴硬脾气大 2024-11-22 14:17:29

在 PHP 5.3 中还有短三元表示法:

$foobar = $fooooooooooooooooooooooooo ?: $bar ;

In PHP 5.3 there is also the short ternary notation:

$foobar = $fooooooooooooooooooooooooo ?: $bar ;
巴黎盛开的樱花 2024-11-22 14:17:29

因为 $foobar = $foo OR $foobar = $bar; 的计算结果如下:

将 foo 分配给 foobar。
* 那里有值吗?
   * 如果没有,则将 bar 分配给 foobar。

而另一个的计算结果是:

foo 有值吗?
* 如果是这样分配 foobar = foo
* else allocate foobar = bar

在第一个示例中,如果 !foo 则分配两次,如果 foo 则可以更快。在第二个示例中,您仅设置一次总计值。我敢打赌,速度差异可以忽略不计。

这里更大的问题是可读性。如果 5.3 短表示法不可用,那么我仍然会使用三元表示法,只是因为其他程序员期望它。

就此而言,通过使用传统的三元语法,您将节省更多的时间和金钱,因为当人们看到您的代码时,他们不会问自己,WTF?

Because $foobar = $foo OR $foobar = $bar; evaluates to this:

Assign foo to foobar.
* Is there a value there?
   * If not, assign bar to foobar.

While the other evaluates to:

Is there a value at foo?
* If so assign foobar = foo
* else assign foobar = bar

In the first example, if !foo you are assigning twice, if foo it can be faster. In the second example, you are only setting the value once total. I'll wager though that the speed difference is negligible.

The bigger issue here is readability. If 5.3 short notation isn't available, then I would still use the ternary notation if only because other programmers expect it.

For that matter, you will save more time and money by using the traditional ternary syntax if only because when people see your code they won't be asking themselves, WTF?

小…楫夜泊 2024-11-22 14:17:29

从 PHP 5.3 开始,有一个特殊的运算符:

$foobar = $fooooooooooooooooooooooooo ?: $bar;

来自 有关三元运算符的文档

从 PHP 5.3 开始,可以省略三元运算符的中间部分。如果 expr1 计算结果为 TRUE,则表达式 expr1 ?: expr3 返回 expr1,并且 expr3否则。

Since PHP 5.3 there is a special operator for this:

$foobar = $fooooooooooooooooooooooooo ?: $bar;

From the documentation on the ternary operator:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

执笔绘流年 2024-11-22 14:17:29

就速度而言,这样:

$foobar = $fooooooooooooooooooooooooo OR $foobar = $bar;

更快

in matter of speed, this:

$foobar = $fooooooooooooooooooooooooo OR $foobar = $bar;

it's faster

清秋悲枫 2024-11-22 14:17:29

不。
我不确定这两种符号是否可以互换,这意味着您不应该使用第二种!无论它们实际上是否相同,如果不能立即清楚代码的作用(如本例所示),您应该更改它,除非您绝对确定没有其他人需要更新此代码。如果我看到这样写的,我不确定它是否应该是这样的,或者是否有一些错别字,而且这个错误根本就没有被发现(见过太多次了!)。

理想情况下,对于很长的变量名,您应该这样做:

if($fooooooooooooooooooooooooo){
    $foobar =$fooooooooooooooooooooooooo;
} else {
    $foobar = $bar;
}

只是为了使其易于阅读,除非速度至关重要,在这种情况下您应该使用三元运算符。

No.
I don't know for sure whether the two notations are interchangeable, which means that you should not use the second one! Whether or not they are actually the same, if it is not immediately clear what the code does (as in this case), you should change it, unless you are absolutely sure that no one else will ever have to update this code. If I saw that written, I wouldn't be sure whether it was supposed to be like this, or whether there was some typo, and the bug was simply never discovered (seen it too many times!).

Ideally, with very long variable names, you should do this:

if($fooooooooooooooooooooooooo){
    $foobar =$fooooooooooooooooooooooooo;
} else {
    $foobar = $bar;
}

Just to make it easy to read, unless speed is of the essence, in which case you should use the ternary operator.

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