Question

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?

Was it helpful?

Solution

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.
Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

OTHER TIPS

Can you update SomeClass:bigQuery() to return a new EmptySet() instead of false?

Then you just have

$foo = SomeClass::bigQuery();

A slight variation of your last option:

$foo = SomeClass::bigQuery() or new EmptySet(); this doesn't actually work, thanks for noticing.

Used often in combination with mySQL code, but seems always forgotten in comparable situations:

$result = mysql_query($sql) or die(mysql_error());

Although personally I prefer one you already mentioned:

if(!$foo = SomeClass::bigQuery())
    $foo = new EmptySet();
$foo = SomeClass::bigQuery();
if (!$foo) $foo = new EmptySet();

Revision two, credit @meagar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top