在PHP中,如果0 == false为true,并且false == false为true,如何测试false?

问题描述

| 我对测试preg_match的返回值特别感兴趣,该返回值可以为1、0或false。     

解决方法

$val === false;
例:
0 === false; // returns false
false === false; // returns true
使用三重等于运算符/严格比较     ,使用
===
类型比较。 检查手册:http://www.php.net/manual/zh/language.operators.comparison.php     ,
$num === 0; //is true if $num is 0 and is an integer
$num === 0; //is false if $num is 0 and is a string
===
检查类型和相等性 所以:
 0 === false; //will return false
 false === false; //will return true
    ,preg_match()和preg_match_all()返回找到的匹配数,如果出错则返回false。但是,这意味着如果未找到匹配项,它将返回0,因此显式测试是否为false,然后尝试遍历一个空集仍然是有问题的。 我通常会测试是否为假,然后在循环结果之前再次测试匹配计数。效果:
$match = preg_match_all($pattern,$subject,$matches);

if($match !== false)
{
    if(count($matches) > 0)
    {
        foreach($matches as $k=>$v)
        {
            ...
        }
    }
    else
    {
        user_error(\'Sorry,no matches found\');
    }
}
else
{
    die(\'Match error\');
}
    ,使用not运算符
!
检查是否为假:
(! 0 === false)
始终为假。