title: Conditionally assigning PHP values
body: <p>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:</p>
  
  <pre><code>$foo = $bar ? $a : b;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$foo = SomeClass::bigQuery() ? SomeClass::bigQuery() : new EmptySet();
  </code></pre>
  
  <p>One option is:</p>
  
  <pre><code>$foo = ($result = SomeClass::bigQuery()) ? $result : new EmptySet();
  </code></pre>
  
  <p>But I'd rather not have the extra <code>$result</code> sitting in memory.</p>
  
  <p>The best option I've got is:</p>
  
  <pre><code>$foo = ($foo = SomeClass::bigQuery()) ? $foo : new EmptySet();
  </code></pre>
  
  <p>Or, without ternary operators:</p>
  
  <pre><code>if(!$foo = SomeClass::bigQuery()) $foo = new EmptySet();
  </code></pre>
  
  <p>Or, if program flow operators are not your style:</p>
  
  <pre><code>($foo = SomeClass::bigQuery()) || ($foo = new EmptySet());
  </code></pre>
  
  <p>So many options, non of them really satisfactory. Which would you use, and am I missing something really obvious here?</p>