Oftentimes when verbosity isn't required, I like to chain ternary operations (inline ifs) together. The resulting code isn't necessarily hard to read with a trained eye. It's for the same reason that doing this:
var x = y || 'default';
in JavaScript can save a lot of extra code and complexity. Ain't nobody got time to check if a variable is undefined.
Here's something that took me a while to realise was the source of a bug though. Would you say these two snippets of code do the same thing?
Java:
public class Main {
public static void main(String []args){
boolean a = true;
boolean b = false;
int output = a?0:b?1:2;
System.out.println(output);
}
}
PHP:
<?php
$a = true;
$b = false;
$output = $a?0:$b?1:2;
echo $output;
?>
Nope. Java does the intuitive (like most other C-like languages) and outputs 0. PHP outputs 2. Why? Java and PHP Order of Operations/Operator Precedence can't tell us anything because we're comparing which of the same thing comes first.
It turns out ternary expressions are evaluated from left to right in PHP. In Java the expression is the same as a?0:(b?1:2)
. To get the same behaviour in PHP, you would need the brackets. As it stands, the expression in PHP is the same as (a?0:b)?1:2
where (unlike in strongly-typed Java) 0 is evaluated as false making the final result 2.
Here's the same code again in C for good measure:
#include <stdio.h>
int main() {
int a = 1;
int b = 0;
int output = (a?0:b)?1:2;
printf("%d", output);
return 0;
}
Yup, outputs a 2 as expected. Beware of these little inconsistencies across syntactically similar languages; you never know when it will cost you hours of debugging!