Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if boolean is true or false

Tags:

php

boolean

I have this little part of code:

$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

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?

I also tried it with:

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

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) "True" Company blockedTrue

like image 533
Matheno Avatar asked Jan 19 '26 10:01

Matheno


1 Answers

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

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

reference: Converting to boolean (PHP Manual)

so, "False" == TRUE !

maybe you can compare them as string:

if(strtoupper($value[1]) == "TRUE"){
    //...
}else{
    //...
}
like image 54
TopCaver Avatar answered Jan 21 '26 01:01

TopCaver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!