6

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;
}
4
  • unclear question, not enough code. Commented Dec 24, 2009 at 3:48
  • I suppose it would be a tad insulting to ask you if count($this->_brokenRulesCollection) is actually non-zero. Commented Dec 24, 2009 at 3:49
  • I'd rather see return ($this->_brokenRulesCollection == 0);, but is the variable ever non-zero? Commented Dec 24, 2009 at 3:50
  • 1
    $this->_brokenRulesCollection is an array of objects. I can force it to be 1 or 0. When I force it to be 1, I get no return value. Perhaps it would be better to ask what I should expect to be returned when it evaluates to false. If I echo out the return value on the calling object when the function returns a true value, I get a 1. Otherwise, when I echo out the return value I get nada. I'm expecting it to be 0 for false. Commented Dec 24, 2009 at 3:55

4 Answers 4

17

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Most obliged, Nicolas. +1 for the reminder to use var_dump. <slaps forehead>
0

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.

3 Comments

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.
Yes. Using empty($this->_brokenRulesCollection) would return false if $this->_brokenRulesCollection is not empty. empty cannot be used as in empty(trim($name)), which would return an error. Also, in PHP, if ((count($array)) == 0) can be written as if (!count($array)), and the code you reported can be simply written as return !count($this->_brokenRulesCollection). For the boolean operators (!, &&, ||), any value that is NOT an empty string, an array without items, an integer equal to zero, NULL, the string '0' is considered equivalent to TRUE.
(follow-up) In example, the expression 1 && "Not empty string" returns TRUE.
0

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.

Comments

-1

if you actually want to see "false", put the value in a variable before you return. as such:

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

1 Comment

This is incorrect; there is no need to store the return value in a variable before returning it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.