False == 0, or not?

Posted by Felix Geisendörfer, on Aug 13, 2007 - in PHP & CakePHP » Other

So far I've always thought false would evaluate to 0 when used in a computational context. Turns out that this isn't always the case.

php
  1. // The world as we know it:
  2. echo 0 + 1; // 1
  3. echo false + 1; // 1
  4. echo 0  > -1; // true
  5.  
  6. // But what is that ...?
  7. echo false  > -1; // false

Now if anybody does have an explanation for this, I'd be glad to hear it. I randomly stumbled upon this when arguing with Mariano today if setting Model::recursive to 'false' has the same effect as setting it to '-1'. Turns out that cake uses a statement like this: if ($recursive > -1) in the core which in turn makes -1 and false do exactly the same thing.

Btw., if you need to work around this behavior you can use something like this:

php
  1. echo (int)false > -1; // true

More posts are to come,
-- Felix