PHP 中的运算符优先级:三元异或赋值

发布于 2024-09-10 02:33:58 字数 873 浏览 0 评论 0原文

在写完我对问题 如何分配到三元运算符中的多个变量 我实际上尝试了我编写的代码:(

true ? $w = 100 xor $r = 200 : $w = 300 xor $r = 400;
var_dump($w); var_dump($r);

别担心它没用,这是理论上的。)

现在,我希望 PHP 能够这样做,根据 运算符优先级

 true  ?   $w = 100  xor  $r = 200   :   $w = 300  xor  $r = 400  ;
(true) ? ( $w = 100  xor  $r = 200 ) : ( $w = 300  xor  $r = 400 );
(true) ? (($w = 100) xor ($r = 200)) : (($w = 300) xor ($r = 400));

当评估三元运算符的第一部分时,应输出:

int 100
int 200

但是我得到了

int 100
int 400

这对我来说很奇怪,因为它需要执行三元运算符的两个部分的部分。

假设这是我的想法有问题。

After writing my response on the question how to assign to multiple variables in a ternary operator I actually tried out the code I wrote:

true ? $w = 100 xor $r = 200 : $w = 300 xor $r = 400;
var_dump($w); var_dump($r);

(Don't bother it's useless, this is theoretic.)

Now, I would expect PHP to do it this way, according to operator precedence:

 true  ?   $w = 100  xor  $r = 200   :   $w = 300  xor  $r = 400  ;
(true) ? ( $w = 100  xor  $r = 200 ) : ( $w = 300  xor  $r = 400 );
(true) ? (($w = 100) xor ($r = 200)) : (($w = 300) xor ($r = 400));

As the first part of the ternary operator is evaluated, this should output:

int 100
int 200

But instead I get

int 100
int 400

This is very odd to me, because it would require that parts of both parts of the ternary operator are executed.

Suppose it's some fault in my thinking.

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

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

发布评论

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

评论(2

非要怀念 2024-09-17 02:33:58

你不是在做吗

(true ? $w = 100 xor $r = 200 : $w = 300) xor $r = 400;

aren't you just doing

(true ? $w = 100 xor $r = 200 : $w = 300) xor $r = 400;
鹿港巷口少年归 2024-09-17 02:33:58

我根本不会以这种方式使用三元运算符。当您需要整个表达式返回值时,请使用三元运算符,而不是替代逻辑代码结构。

例如:

if (true) {
  $w = 100;
  $r = 200;
} else {
  $w = 300;
  $r = 400;
}

var_dump($w); 
var_dump($r);

使用 if/else 结构的优点:

  • 更容易阅读、更容易维护、更容易调试。
  • 如果需要,可以更轻松地在每个条件块内添加更多步骤。
  • 如果您使用代码覆盖率工具运行测试,则当所有代码不在一行上时,您可以更准确地了解正在测试哪些代码路径。
  • 您不必向 Stack Overflow 发布问题即可使其正常工作!

使用三元运算符的优点:

  • 使用更少的大括号和分号,以防您用完它们或在键盘上找不到它们。

I wouldn't use the ternary operator in this way at all. Use the ternary operator when you need the whole expression to return a value, not as a substitute for logical code constructs.

For example:

if (true) {
  $w = 100;
  $r = 200;
} else {
  $w = 300;
  $r = 400;
}

var_dump($w); 
var_dump($r);

Advantages of using if/else construct:

  • Easier to read, easier to maintain, easier to debug.
  • Easier to add more steps inside each conditional block if the need arises.
  • If you run tests using code coverage tools, you get a more accurate view of which code paths are being tested when all your code isn't on one line.
  • You don't have to post a question to Stack Overflow to get it working!

Advantages of using ternary operator:

  • Uses fewer curly-braces and semicolons, in case you're running out of them or can't find them on your keyboard.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文