views:

93

answers:

4

I cannot get a false value to return here. A true value returns fine. What am I missing?

if ((count($this->_brokenRulesCollection)) == 0)  {
    return true;
} else {
    return false;
}
A: 

Well what is a $this->_brokenRulesCollection? is it an array or an object? if an object does it implement Countable or Iterator?

prodigitalson
Please make this a commment, not an answer.
Asaph
A: 

The code can just return false if the array $this->_brokenRulesCollection is not empty.

If $this->_brokenRulesCollection is not an array or an object with implemented Countable interface, then count($this->_brokenRulesCollection) will returned 1.

kiamlaluno
So, would it be better to test for empty instead?Basically, I just need to know if the array has anything in it and if it does, it should return false.
Stepppo
kiamlaluno
kiamlaluno
+5  A: 

In PHP, false when converted to a string is an empty string, and true converted to a string is "1".

Use var_dump instead of echo for debugging.

Nicolás
Most obliged, Nicolas. +1 for the reminder to use var_dump. <slaps forehead>
Stepppo
A: 

In conditional expressions (unless you use the type matching operators === or !==) any non-zero integer is equivalent to true, any non-zero length string is equivalent to true and conversely, zero or a blank string are false.

So

if ((count($this->_brokenRulesCollection)) == 0)  {
   return true;
} else {
  return false;
}

Can be written as just:

return count($this->_brokenRulesCollection);

C.

symcbean