php not returning value of false
I cannot get a false value to return here. A true value returns fine. What am I missing?
if ((count($this->_b开发者_运维百科rokenRulesCollection)) == 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.
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;
精彩评论