Php does not return false

I cannot get a false value to return here. True value returns a penalty. What am I missing?

if ((count($this->_brokenRulesCollection)) == 0)  {
    return true;
} else {
    return false;
}
+3
source share
4 answers

In PHP, falsewhen converting to a string, there will be an empty string, and trueconverted to a string equal to "1".

Use var_dumpinstead echofor debugging.

+12
source

The code may simply return falseif the array is $this->_brokenRulesCollectionnot empty.

If $this->_brokenRulesCollectionit is not an array or object with a implemented counter interface, then it count($this->_brokenRulesCollection)will return.

0
source

( === ! ==) , , .

So

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

:

return count($this->_brokenRulesCollection);

.

0

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

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

Source: https://habr.com/ru/post/1726510/


All Articles