I have often used the following construct in Javascript:
var foo = other_var || "default_value";
In Javascript, if the left side is falsy, then the value on the right side is assigned.
It is very handy, and saves writing longer and unnecessarily explicit ternary expressions.
Is there a name for this sort of construct ?
Bonus: Is there a trick to do this in Php without using a ternary operator?
PS: another variant is to throw an error if you don't get a truthy value, instead of giving a default value:
var foo = something || alert("foo is not set!");
return var_result || false;
If the result of the code is truthy it is returned as is, otherwise you are sure to return false as per documentation, or for consistency in your api. But yeah, it can be done with a ternary operator, only far less cool. – Axiology