gpt4 book ai didi

php - 检查 boolean 值是真还是假

转载 作者:行者123 更新时间:2023-12-02 07:02:49 25 4
gpt4 key购买 nike

我有一小部分代码:

$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 语句时它返回:“Company blocked”,所以 $value[1] 是 TRUE 而它需要是 FALSE。

有人能告诉我为什么它没有返回正确的值吗?

我也试过了:
if ($value[1] == 1){
echo "Company blocked";
}
else{
echo "Company NOT blocked";
}

这给了我 FALSE 的值,但是 if 语句以某种方式将其更改为 TRUE。
var_dump($value[1]) 

当我尝试一家正常被封锁的公司时,它给了我正确的结果:
string(4) "True" Company blockedTrue

最佳答案

$value的类型1是“字符串”而不是“boolean ”。

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

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



引用: Converting to boolean (PHP Manual)

所以, “假” == 真 !

也许您可以将它们作为字符串进行比较:
if(strtoupper($value[1]) == "TRUE"){
//...
}else{
//...
}

关于php - 检查 boolean 值是真还是假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com