For the very common case of assigning a value to a variable based on the outcome of an expression I'm a fan of ternary operators:
$foo = $bar ? $a : b;
However, if $bar is a relatively expensive operation and I want to assign the result of $bar to $foo if the result is truthy, this is inefficient:
$foo = SomeClass::bigQuery() ? SomeClass::bigQuery() : new EmptySet();
One option is:
$foo = ($result = SomeClass::bigQuery()) ? $result : new EmptySet();
But I'd rather not have the extra $result
sitting in memory.
The best option I've got is:
$foo = ($foo = SomeClass::bigQuery()) ? $foo : new EmptySet();
Or, without ternary operators:
if(!$foo = SomeClass::bigQuery()) $foo = new EmptySet();
Or, if program flow operators are not your style:
($foo = SomeClass::bigQuery()) || ($foo = new EmptySet());
So many options, non of them really satisfactory. Which would you use, and am I missing something really obvious here?
PHP 5.3 introduced a new syntax to solve exactly this problem:
$x = expensive() ?: $default;
See the documentation:
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.
Expressionexpr1 ?: expr3
returnsexpr1
ifexpr1
evaluates toTRUE
, andexpr3
otherwise.
Can you update SomeClass:bigQuery() to return a new EmptySet() instead of false?
Then you just have
$foo = SomeClass::bigQuery();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With