In javascript I can write something like:
var result = Whatever() || [];
and if Whatever() returns null or undefined, the result variable will contain an empty array.
Is there a similar shortcut in PHP? This will just return false:
$result = Whatever() || array();
I can use a ternary operator, but since there is a function call, I will need to create an additional variable like so:
$whatever = Whatever();
$result = $whatever ? $whatever : array();
which seems ugly and not very readable. Is there a better pattern?
return Whatever() ? Whatever() : [];orreturn Whatever() ? Whatever() : array()- as you said, those are your best bets - it's called a Ternary Operator or shortcutted : davidwalsh.name/php-shorthand-if-else-ternary-operators