简体   繁体   English

php没有返回false的值

[英]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;
}

In PHP, false when converted to a string is an empty string, and true converted to a string is "1". 在PHP中,转换为字符串时为false是空字符串, true转换为字符串为“1”。

Use var_dump instead of echo for debugging. 使用var_dump而不是echo进行调试。

The code can just return false if the array $this->_brokenRulesCollection is not empty. 如果数组$this->_brokenRulesCollection不为空,则代码可以返回false

If $this->_brokenRulesCollection is not an array or an object with implemented Countable interface, then count($this->_brokenRulesCollection) will returned 1. 如果$this->_brokenRulesCollection不是数组或具有已实现Countable接口的对象,则count($this->_brokenRulesCollection)将返回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. 在条件表达式中(除非使用类型匹配运算符===或!==),任何非零整数等于true,任何非零长度字符串等效于true,相反,零或空字符串为false。

So 所以

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

Can be written as just: 可以写成:

return count($this->_brokenRulesCollection);

C. C。

if you actually want to see "false", put the value in a variable before you return. 如果您确实想要看到“false”,请在返回之前将值放入变量中。 as such: 因此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM