Stepppo
Stepppo

Reputation: 347

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->_brokenRulesCollection)) == 0)  {
    return true;
} else {
    return false;
}

Upvotes: 6

Views: 4929

Answers (4)

Aous1000
Aous1000

Reputation: 2192

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;

Upvotes: -1

symcbean
symcbean

Reputation: 48387

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.

Upvotes: 0

Nicolás
Nicolás

Reputation: 7533

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.

Upvotes: 17

avpaderno
avpaderno

Reputation: 29748

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.

Upvotes: 0

Related Questions