title: Is it possible to make a smart ternary operator to return a complex value if a condition is met?
body: <p>Apologies for the vague Title. I have no idea how to word the question!</p>
  <p>In both PHP and JS I come across situations where I want to return a value but only if it meets a criteria. I was wondering if there is shorter way that I have not discovered yet?</p>
  <hr />
  <p><strong>Example: if complexFunction() &gt;= 0 then return its value else return zero</strong></p>
  <p>The complexFunction() return value may be used in multiple places in different ways so I do not want to include the condition in the function itself</p>
  <p><code>return (complexFunction() &gt;= 0 ? complexFunction() : 0);</code></p>
  <p><em>//inefficient - calls function twice</em></p>
  <pre><code>$value = complexFunction();
  return ($value &gt;= 0 ? $value : 0);
  </code></pre>
  <p><em>//takes 2 lines and creates a variable</em></p>
  <p><code>return (complexFunction() ? &gt;= 0 : 0);</code></p>
  <p><em>//ideal - complexFunction is only executed once, no variable is manually assigned and only one line used</em></p>
  <hr />
  <p><strong>This also would help in Excel:</strong></p>
  <p><code>=if(sum(ComplexRange) &gt; 1000, sum(ComplexRange), 1000)</code></p>
  <p><em>//inefficient - performs complex process twice</em></p>
  <p><code>=if(HelperColumnSum &gt; 1000, HelperColumnSum, 1000)</code></p>
  <p><em>//increases size of sheet</em></p>
  <p><code>=returnifconditionelse(sum(ComplexRange), &quot;&gt;1000&quot;, 1000)</code></p>
  <p><em>//ideal - no helpercolumn or repeated calculation</em></p>