检查布尔值是对还是错 [英] Check if boolean is true or false

查看:81
本文介绍了检查布尔值是对还是错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这部分代码:

$company = array('relation' => $_SESSION['username']);
$companyresponse = $wcfclient->GetCompany($company);
    foreach ($companyresponse->GetCompanyResult as $key => $value){
    echo $value[0]; //This is the name of the company

    if ($value[1] == TRUE){
       echo "Company blocked";
    }
    elseif($value[1] == FALSE){
       echo "Company NOT blocked";
    }
    echo $value[1]; //This gives me the correct result, in this case: FALSE

$ value [1]的结果为FALSE,尽管它通过我的if语句时返回:"Companyblocked",所以$ value [1]为TRUE时它必须为FALSE.

The result of $value[1] is FALSE, though when it passed my if-statement it returns: "Company blocked" , so $value[1] is TRUE while it needs to be FALSE.

有人可以告诉我为什么它不返回正确的值吗?

Can someone tell me why it doesn't return the correct value?

我也尝试过:

if ($value[1] == 1){
   echo "Company blocked";
}
else{
   echo "Company NOT blocked";
}

这给了我值FALSE,但是某种程度上,如果if语句将其更改为TRUE.

This gives me the value FALSE, but somehow the if-statement changes it into TRUE.

var_dump($value[1]) 

当我尝试正常被禁止的公司时,它会给我以下正确的结果:

When I try a company which is blocked normally it gives me the correct result of:

string(4)真"公司受阻真

推荐答案

$ value的类型

The type of $value1 is "String" NOT "bool".

转换为布尔值时,将考虑以下值否:

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

其他所有值均视为TRUE(包括任何资源).

Every other value is considered TRUE (including any resource).

参考:转换为布尔值(PHP手册)

如此,假" ==真

也许您可以将它们作为字符串进行比较:

maybe you can compare them as string:

if(strtoupper($value[1]) == "TRUE"){
    //...
}else{
    //...
}

这篇关于检查布尔值是对还是错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆