PHP 中的三元运算符和变量重新赋值

发布于 2024-10-11 18:07:38 字数 396 浏览 6 评论 0原文

我仔细研究了关于三元运算符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 技术交流群。

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

发布评论

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

评论(4

倒带 2024-10-18 18:07:38

我不知道你的第一个例子是否效率低下,但它确实毫无意义。我仍然认为 if 语句更清晰:

$foo = 'bar';

if (strlen($foo) > 3)
    $foo = substr($foo, 0, 3);

虽然以下内容有效,但在末尾放置 null 是没有意义的,因为三元运算符旨在用于计算表达式/值,但这里 < code>null 除了防止解析错误之外什么也不做:

!defined('SECURE') ? exit : null;

更常见的是,您会看到这个,布尔短路的示例(如果 则不执行 or exit ) SECURE 未定义,因为一旦发现至少一个条件为真,or 条件表达式就会自动计算为 true):

defined('SECURE') or exit;

我想要表达的观点是:不要'不要仅仅因为可以就使用三元条件表达式。

I don't know if your first example is inefficient, but it sure is pointless. I still think an if statement is clearer:

$foo = 'bar';

if (strlen($foo) > 3)
    $foo = substr($foo, 0, 3);

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 here null does nothing other than to prevent a parse error:

!defined('SECURE') ? exit : null;

More commonly, you would see this, an example of boolean short-circuiting (or exit doesn't execute if SECURE is not defined, because the or conditional expression evaluates to true automatically once at least one condition is found to be true):

defined('SECURE') or exit;

The point I'm trying to make is this: don't use ternary conditional expressions just because you can.

守望孤独 2024-10-18 18:07:38

在这种情况下,我使用 BoltClock 提供的形式:

if (strlen($foo) > 3) {
    $foo = substr($foo, 0, 3);
}

PHP 没有实现在这种情况下更简单的东西,但是:/

In this cases, I use the form presented by BoltClock:

if (strlen($foo) > 3) {
    $foo = substr($foo, 0, 3);
}

PHP does not implement something more simple to work in this cases, yet :/

东风软 2024-10-18 18:07:38

上面已经讨论了这里使用三元并不是最佳的主题。我将解决您关于是否会重新分配该值的问题:

这取决于您所说的“重新分配”。 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 the refcount 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).

绝影如岚 2024-10-18 18:07:38

总是存在短路,尽管正如 @BoltClock 所说,在我看来, if 语句可能更具可读性,并且为 else ifelse 打开了大门 条件也是如此。

strlen($foo) > 3 && $foo = substr($foo, 0, 3); 

仅当前一个语句的计算结果为 TRUE 时,才会执行后一个语句。

There is always short-circuiting, although as @BoltClock said, an if statement is probably more readable in my opinion, and opens the door to else if and else conditions as well.

strlen($foo) > 3 && $foo = substr($foo, 0, 3); 

The latter statement will only be executed if the former evaluates to TRUE.

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