In PHP the ternary operator has a short version.
expr1 ? expr2 : expr3;
changes into
expr1 ? : expr3;
The short version returns the result of expr1 on true and expr3 on false. This allows cool code that can populate variables based on their own current state. For example:
$employee = $employee ? : new Employee();
If $employee == null
or evaluates to false for any other reason, the code above will create a new Employee();
Otherwise the value in $employee
will be reassigned to itself.
I was looking for something similar in Java, but I could not find any similar use case of the ternary operator. So I am asking if is there such a functionality or something similar that can avoid one of the expressions of the ternary operator in order to reduce duplication.