是否可以用一个三元运算符设置多个变量?

发布于 2024-09-10 17:15:57 字数 735 浏览 11 评论 0原文

我想看看是否可以用一个三元运算符设置多个变量。我用谷歌搜索了一下,但没有想到任何东西。我开始测试一些想法,并发现了一些接近的东西——但也出现了一些奇怪的行为;关于发生了什么事有什么想法吗?并且,是否可以在一次三元运算中设置多个 var ?如果是这样,有正确的方法吗?

$i=9;
($i==9)?($w=3|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 3 r= 2


$i=9;
($i==9)?($w=4|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 6 r= 2


$i=9;
($i==9)?($w=3|$r=7):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 7 r= 7


$i=444;
($i==9)?($w=4|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 7 r= 1


$i=444;
($i==9)?($w=4|$r=2):($w=1|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 1 r= 1 

谢谢...


编辑:

我做了更多测试,发现这可以正常工作:

    ($i==9)?($w=4 AND $r=7):($w=7 AND $r=1);

但是,我不确定这是否正确。我很好奇第一个例子中发生了什么。

I was looking to see if it's possible to set multiple variables with one ternary operator. I google'd a bit, but didn't come up with anything. I started testing a few ideas, and found something close -- but also getting some strange behavior; any ideas as to what's going on? And, is it possible to set more than one var in a single ternary operation? If so, is there a proper way of doing it?

$i=9;
($i==9)?($w=3|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 3 r= 2


$i=9;
($i==9)?($w=4|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 6 r= 2


$i=9;
($i==9)?($w=3|$r=7):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 7 r= 7


$i=444;
($i==9)?($w=4|$r=2):($w=7|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 7 r= 1


$i=444;
($i==9)?($w=4|$r=2):($w=1|$r=1);
echo 'w= '.$w.' r= '.$r;//w= 1 r= 1 

Thanks...


Edit:

I did a little more testing, and found that this works correctly:

    ($i==9)?($w=4 AND $r=7):($w=7 AND $r=1);

however, I'm not sure if this is correct. And I'm curious as to what's going on in the first example.

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

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

发布评论

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

评论(4

深海不蓝 2024-09-17 17:15:57

正如 Chadwick 所解释的,使用 ANDOR 逻辑运算符不会按照您希望的方式工作:

$i = 0; $w = 0; $r = 0;
($i==9) ? ($w=4 AND $r=7) : ($w=7 AND $r=1);
echo "w = $w, r = $r\n";
// w = 7, r = 1

$i = 0; $w = 0; $r = 0;
($i==9) ? ($w=0 AND $r=7) : ($w=0 AND $r=1);
echo "w = $w, r = $r\n";
// w = 0, r = 0

但是,如果您确实想在一个语句中进行多个赋值,您可以使用 list 构造:

$i = 0; $w = 0; $r = 0;
($i==9) ?
    (list($w, $r) = array(4, 7)) :
    (list($w, $r) = array(7, 1));
echo "w = $w, r = $r\n";
// w = 7, r = 1

$i = 0; $w = 0; $r = 0;
($i==9) ?
    (list($w, $r) = array(0, 7)) :
    (list($w, $r) = array(0, 1));
echo "w = $w, r = $r\n";
// w = 0, r = 1

As Chadwick has explained, using the AND or OR logical operators will not work the way you want them to:

$i = 0; $w = 0; $r = 0;
($i==9) ? ($w=4 AND $r=7) : ($w=7 AND $r=1);
echo "w = $w, r = $r\n";
// w = 7, r = 1

$i = 0; $w = 0; $r = 0;
($i==9) ? ($w=0 AND $r=7) : ($w=0 AND $r=1);
echo "w = $w, r = $r\n";
// w = 0, r = 0

However, if you really want to make multiple assignments in one statement, you can use the list construct:

$i = 0; $w = 0; $r = 0;
($i==9) ?
    (list($w, $r) = array(4, 7)) :
    (list($w, $r) = array(7, 1));
echo "w = $w, r = $r\n";
// w = 7, r = 1

$i = 0; $w = 0; $r = 0;
($i==9) ?
    (list($w, $r) = array(0, 7)) :
    (list($w, $r) = array(0, 1));
echo "w = $w, r = $r\n";
// w = 0, r = 1
零時差 2024-09-17 17:15:57

OR 运算符是您在三元表达式中进行的第二个赋值的快捷方式。因此,如果第一个赋值为非零,则不会进行第二个赋值。

如果切换到 AND 语句(或 &&),表达式中的第二个赋值仍然依赖于第一个:如果第一个赋值为 0 (即 false),则不计算 AND 的后半部分。这可能比第一个更难调试,因为第一个值很少为零。它可能永远不会,而且这将永远有效...直到有一天...

三元运算符只是 if...else... 的简写,具有单个表达式而不是多个语句。如果您想要多个独立作业,我强烈建议您使用标准的 if...else... 更容易阅读。

The OR operator is shortcutting the second assignments you're making within the expressions of your ternary. So the second assignment isn't getting made if the first evaluates to non zero.

If you switch to an AND statment (or &&) the second assignment in the expression is still dependant on the first: if the first evaluates to 0 (i.e. false), then the second half of the AND is not evaluated. That's probably a more difficult one to debug than the first, since it's probably rare that the first value will be zero. It may never be, and this will always work... until that one day...

The ternary operator is just shorthand for if...else... with single expressions rather than multiple statements. If you want multiple, independent assignments I highly recommend going with standard if...else... Easier to read all around.

赢得她心 2024-09-17 17:15:57

我将尝试解释为什么您当前的代码的行为如此。
首先要知道:| 是按位或运算符。第二件事要知道:| 的运算符优先级高于 =,因此首先执行。

那么,让我们看一下第一个代码:$w=3|$r=2。根据运算符优先级,此代码可以这样编写:$w=(3|$r=2)。因此 $r get 设置正确,然后执行 $w=3|2 。双系统中的311210

  11
| 10
====
  11

所以结果是正确的。

现在,让我们看第二个代码:

$w=4|$r=2$w=(4|$r=2) 相同。 $r 设置正确,则执行 $w=4|2。双系统中41002010

  100
| 010
=====
  110

而双系统中110十进制是6

所以,现在您知道错误的值来自哪里,现在让我们找到一个解决方案:

不鼓励在此处使用 && ,因为如果第一个值设置为 0第二个根本不会被设置。此外,它的运算符优先级错误:$w=1&&$r=2$w=(1&&$r=2) 相同。因此,$w 始终会被指定为 truefalse

一种可能的解决方案是使用xor 运算符。该运算符要求双方都执行,并且具有完美的优先级:

$i == 9 ? ($w = 1 xor $r = 2) : ($w = 2 xor $r = 3);

但是,您可能会想象,这很难阅读,所以最好不要使用它......

I am going to try an explain why your current code behaves like it behaves.
First thing to know: | is the bitwise-or operator. Second thing to know: | has a higher operator precendance than = and therefore is executed first.

So, let's have a look at the first code: $w=3|$r=2. According to operator precedence this code may be written this way: $w=(3|$r=2). So $r get's set correctly and then $w=3|2 is performed. 3 in the dual system is 11 and 2 is 10.

  11
| 10
====
  11

So the result is correct.

Now, let's look at the second code:

$w=4|$r=2 is same as $w=(4|$r=2). $r is set correctly, then $w=4|2 is performed. 4 is 100 in dual system, 2 is 010:

  100
| 010
=====
  110

And 110 in the decimal system is 6!

So, now you know there the wrong values come from, now let's find a solution:

Using && here is discouraged, because if the first value was set to 0 the second wouldn't be set at all. Furthermore it has wrong operator precedence: $w=1&&$r=2 is same as $w=(1&&$r=2). So $w would always be assigned either true or false.

One possible solution would be to use the xor operator. This operator requires both sides to be executed and it has the perfect precendance:

$i == 9 ? ($w = 1 xor $r = 2) : ($w = 2 xor $r = 3);

But, you may imagine, this is hard to read, so better not use it...

东走西顾 2024-09-17 17:15:57

您缺少一个管道字符来形成 OR 子句:

($i==9)?($w=3|$r=2):($w=7|$r=1);

应该是:

($i == 9) ? ($w = 3 || $r = 2) : ($w = 7 || $r = 1);

是的,可以看出您有多个变量 $w$r 在那里使用。

You are missing a pipe character to form an OR clause:

($i==9)?($w=3|$r=2):($w=7|$r=1);

It should be:

($i == 9) ? ($w = 3 || $r = 2) : ($w = 7 || $r = 1);

And yes as can be seen you have more than one variable $w and $r used there.

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