Ternary operator associativity
The ternary operator is left-associative and therefore behaves entirely incorrectly:
$ cat ternary.php <?php echo (FALSE ? "a" : FALSE ? "b" : "c")."\n"; echo (FALSE ? "a" : TRUE ? "b" : "c")."\n"; echo (TRUE ? "a" : FALSE ? "b" : "c")."\n"; echo (TRUE ? "a" : TRUE ? "b" : "c")."\n"; ?> $ php ternary.php c b b b
In any other language with a ternary operator, you can stack them and build an if-elseif-elseif-else expression:
$ cat ternary.pl #!/usr/bin/perl -w use strict; print +(0 ? "a" : 0 ? "b" : "c")."\n"; print +(0 ? "a" : 1 ? "b" : "c")."\n"; print +(1 ? "a" : 0 ? "b" : "c")."\n"; print +(1 ? "a" : 1 ? "b" : "c")."\n"; $ perl ternary.pl c b a a
$ cat ternary.c #include <stdio.h> int main() { printf("%s\n", 0 ? "a" : 0 ? "b" : "c"); printf("%s\n", 0 ? "a" : 1 ? "b" : "c"); printf("%s\n", 1 ? "a" : 0 ? "b" : "c"); printf("%s\n", 1 ? "a" : 1 ? "b" : "c"); } $ gcc -o ternary ternary.c; ./ternary c b a a