In Ruby:
a = true ? :a : true ? :b : :c
p a
Guess the answer! Yes, as you thought, the answer is :a
.
In JavaScript:
var a = true ? 'a' : true ? 'b' : 'c';
alert(a);
Guess the answer! Yes, as you thought, the answer is a
.
In Vim script:
let a = 1 ? 'a' : 1 ? 'b' : 'c'
echo a
Guess the answer! Yes, as you thought, the answer is a
.
In PHP:
$a = true ? 'a' : true ? 'b' : 'c';
print_r($a);
Guess the answer! No, the answer if "b"
.
$a = true ? 'a' : (true ? 'b' : 'c');
print_r($a);
The code above is the equivalent code to the examples in Ruby, JS and Vim script. It's impossibly difficult to imagine why the specification is so.
So, In PHP the precedence works out like:?
ReplyDelete$a = (true ? 'a' : true) ? 'b' : 'c';
Weird.
Yes it is. It's the weirdest ever.
ReplyDelete