Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally assigning PHP values

Tags:

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?

like image 478
Hamish Avatar asked Dec 01 '10 22:12

Hamish


2 Answers

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.

like image 66
meagar Avatar answered Dec 13 '22 12:12

meagar


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

Then you just have

$foo = SomeClass::bigQuery(); 
like image 40
Jeff Davis Avatar answered Dec 13 '22 12:12

Jeff Davis