In PHP, the ternary operator has a short version.
expr1 ? expr2 : expr3;
changes to
expr1 ? : expr3;
The short version returns expr1 to true and expr3 to false. This allows you to use cool code that can populate variables depending on their current state. For instance:
$employee = $employee ? : new Employee();
If $employee == null or false for any other reason, the code above will create 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 a similar example of using a ternary operator. So I ask if there is such functionality or something similar that avoids one of the ternary operator expressions in order to reduce duplication.
source share