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;
}
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.
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.
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.1 && "Not empty string" returns TRUE.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.
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;
return ($this->_brokenRulesCollection == 0);, but is the variable ever non-zero?