php: $a=$b OR $a=$c 与三元
我需要将两个变量之一分配给第三个变量,如果第一个变量为 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 PHP 5.3 中还有短三元表示法:
In PHP 5.3 there is also the short ternary notation:
因为
$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?
从 PHP 5.3 开始,有一个特殊的运算符:
来自 有关三元运算符的文档:
Since PHP 5.3 there is a special operator for this:
From the documentation on the ternary operator:
就速度而言,这样:
更快
in matter of speed, this:
it's faster
不。
我不确定这两种符号是否可以互换,这意味着您不应该使用第二种!无论它们实际上是否相同,如果不能立即清楚代码的作用(如本例所示),您应该更改它,除非您绝对确定没有其他人需要更新此代码。如果我看到这样写的,我不确定它是否应该是这样的,或者是否有一些错别字,而且这个错误根本就没有被发现(见过太多次了!)。
理想情况下,对于很长的变量名,您应该这样做:
只是为了使其易于阅读,除非速度至关重要,在这种情况下您应该使用三元运算符。
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:
Just to make it easy to read, unless speed is of the essence, in which case you should use the ternary operator.