PHP 中的三元运算符和变量重新赋值
我仔细研究了关于三元运算符与if/else结构的问题,虽然我知道在正常情况下使用三元运算符不会带来性能损失/增益if/else 结构,我还没有看到任何提及这种情况的信息。 PHP 特定的语言(但欢迎任何与语言无关的细节)解释器在这样的情况下会重新分配值:
$foo = 'bar'
$foo = strlen($foo) > 3 ? substr($foo, 0, 3) : $foo;
因为这将计算为 $foo = $foo;
是这样低效的,还是解释器只是简单地忽略/放弃这个评估?
顺便说一句,怎么样:
!defined('SECURE') ? exit : null;
I've perused the questions on ternary operators vs. if/else structures, and while I understand that under normal circumstances there is no performance loss/gain in using ternary operators over if/else structures, I've not seen any mention of this situation. Language specific to PHP (but any language agnostic details are welcome) does the interpreter reassign values in situations like this:
$foo = 'bar'
$foo = strlen($foo) > 3 ? substr($foo, 0, 3) : $foo;
Since this would evaluate to $foo = $foo;
is this inefficient, or does the interpreter simply overlook/discard this evaluation?
On a side note, what about:
!defined('SECURE') ? exit : null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道你的第一个例子是否效率低下,但它确实毫无意义。我仍然认为 if 语句更清晰:
虽然以下内容有效,但在末尾放置
null
是没有意义的,因为三元运算符旨在用于计算表达式/值,但这里 < code>null 除了防止解析错误之外什么也不做:更常见的是,您会看到这个,布尔短路的示例(如果
则不执行
未定义,因为一旦发现至少一个条件为真,or exit
) SECUREor
条件表达式就会自动计算为 true):我想要表达的观点是:不要'不要仅仅因为可以就使用三元条件表达式。
I don't know if your first example is inefficient, but it sure is pointless. I still think an if statement is clearer:
And while the following works, it makes no sense to place
null
at the end because a ternary operator is meant to be used to evaluate expressions/values, but herenull
does nothing other than to prevent a parse error:More commonly, you would see this, an example of boolean short-circuiting (
or exit
doesn't execute ifSECURE
is not defined, because theor
conditional expression evaluates to true automatically once at least one condition is found to be true):The point I'm trying to make is this: don't use ternary conditional expressions just because you can.
在这种情况下,我使用 BoltClock 提供的形式:
PHP 没有实现在这种情况下更简单的东西,但是:/
In this cases, I use the form presented by BoltClock:
PHP does not implement something more simple to work in this cases, yet :/
上面已经讨论了这里使用三元并不是最佳的主题。我将解决您关于是否会重新分配该值的问题:
这取决于您所说的“重新分配”。 PHP 不会优化,因此将评估
$foo = $foo
。另一方面,这不会导致 PHP 将 $foo 的值复制到新的内存块。也许 PHP 只会增加$foo
上的refcount
,然后立即减少它(尽管我不确定自分配的确切实现细节)。因此,即使 PHP 会执行该语句,它也不会影响性能(除非您选择在代码中写入$foo = $foo
七百万次)。The topic that using a ternary here is not optimal has already been covered above. I'm going to address your question about whether it will reassign the value:
This depends on what you call "reassigning". PHP does not optimize, so the
$foo = $foo
will be evaluated. On the other hand this will not cause PHP to copy the value of$foo
to a new chunk of memory. Probably PHP will just increase therefcount
on$foo
and then immediately decrease it (though I'm not sure about the exact implementation details of self-assignment). So, even though PHP will execute the statement, it won't affect performance (unless you choose to write$foo = $foo
seven million times in your code).总是存在短路,尽管正如 @BoltClock 所说,在我看来,
if
语句可能更具可读性,并且为else if
和else 打开了大门
条件也是如此。仅当前一个语句的计算结果为 TRUE 时,才会执行后一个语句。
There is always short-circuiting, although as @BoltClock said, an
if
statement is probably more readable in my opinion, and opens the door toelse if
andelse
conditions as well.The latter statement will only be executed if the former evaluates to TRUE.