是否可以用一个三元运算符设置多个变量?
我想看看是否可以用一个三元运算符设置多个变量。我用谷歌搜索了一下,但没有想到任何东西。我开始测试一些想法,并发现了一些接近的东西——但也出现了一些奇怪的行为;关于发生了什么事有什么想法吗?并且,是否可以在一次三元运算中设置多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Chadwick 所解释的,使用
AND
或OR
逻辑运算符不会按照您希望的方式工作:但是,如果您确实想在一个语句中进行多个赋值,您可以使用
list
构造:As Chadwick has explained, using the
AND
orOR
logical operators will not work the way you want them to:However, if you really want to make multiple assignments in one statement, you can use the
list
construct: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 to0
(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.我将尝试解释为什么您当前的代码的行为如此。
首先要知道:
|
是按位或运算符。第二件事要知道:|
的运算符优先级高于=
,因此首先执行。那么,让我们看一下第一个代码:
$w=3|$r=2
。根据运算符优先级,此代码可以这样编写:$w=(3|$r=2)
。因此$r
get 设置正确,然后执行$w=3|2
。双系统中的3
为11
,2
为10
。所以结果是正确的。
现在,让我们看第二个代码:
$w=4|$r=2
与$w=(4|$r=2)
相同。$r
设置正确,则执行$w=4|2
。双系统中4
为100
,2
为010
:而双系统中
110
十进制是6
!所以,现在您知道错误的值来自哪里,现在让我们找到一个解决方案:
不鼓励在此处使用
&&
,因为如果第一个值设置为0
第二个根本不会被设置。此外,它的运算符优先级错误:$w=1&&$r=2
与$w=(1&&$r=2)
相同。因此,$w
始终会被指定为true
或false
。一种可能的解决方案是使用
xor
运算符。该运算符要求双方都执行,并且具有完美的优先级:但是,您可能会想象,这很难阅读,所以最好不要使用它......
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 is11
and2
is10
.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
is100
in dual system,2
is010
:And
110
in the decimal system is6
!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 to0
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 eithertrue
orfalse
.One possible solution would be to use the
xor
operator. This operator requires both sides to be executed and it has the perfect precendance:But, you may imagine, this is hard to read, so better not use it...
您缺少一个管道字符来形成
OR
子句:应该是:
是的,可以看出您有多个变量
$w
和$r
在那里使用。You are missing a pipe character to form an
OR
clause:It should be:
And yes as can be seen you have more than one variable
$w
and$r
used there.