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.
As Chadwick has explained, using the
ANDorORlogical 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
listconstruct: