title: What do PHP closures return in IF statements?
body: <p>My goal is to put some complex logic in an if() statement. Let's say I have an array of values and I'm going to execute a bit of code if everything in my array is nonzero. Normally, I could say, <code>$valid = true</code>, <code>foreach</code> my array, and set <code>$valid = false</code> when a zero is found. Then I'd run my code <code>if ($valid)</code>. Alternatively, I could put my loop into a function and put the function intop my <code>if()</code>.</p>
  
  <p>But I'm lazy, so I'd rather not muck about with a bunch of "valid" flags, and I'd rather not write a whole new function that's only being used in one place.</p>
  
  <p>So let's say I have this:</p>
  
  <pre><code>if ($q = function() { return 'foo'; }) {
    echo $q;
  }
  else {
    echo 'false';
  }
  </code></pre>
  
  <p>I was expecting that the <code>if</code> gets <code>'foo'</code>, which evaluates to true. My closure is committed to <code>$q</code> and the statement executes. <code>$q</code> returns string <code>foo</code>, and 'foo' is printed. </p>
  
  <p>Instead, I get the error <code>Object of class Closure could not be converted to string</code>.</p>
  
  <p>So let's try this instead:</p>
  
  <pre><code>if (function() { return false; }) {
    echo 'foo';
  }
  else {
    echo 'true';
  }
  </code></pre>
  
  <p>I was expecting that my function would return false and 'true' would be printed. Instead, 'foo' is printed.</p>
  
  <p>What is wrong about the way that I'm going about this? It seems like it's saying, "Yep, that sure is a function!" instead of "No, because the function evaluated to false." Is there a way to do what I'm trying to do?</p>