Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does php cast boolean variables?

How does php cast boolean variables?

I was trying to save a boolean value to an array:

$result["Users"]["is_login"] = true;

but when I use debug the is_login value is blank. and when I do conditionals like:

if($result["Users"]["is_login"])

the conditions are always false.

Then i tried doing this:

$result["Users"]["is_login"] = "true";

and it worked.

It's not much of a big deal but when I'm returning boolean values from functions i still have to convert them to strings.

like image 899
zero juan Avatar asked Jun 17 '26 09:06

zero juan


1 Answers

there is no cast

the

if($result["Users"]["is_login"])

should work. can you try to use var_dump($result["Users"]["is_login"]); to make sure the variable has been set properly.

you can check is a variable is set or not by using the isset (manual) function.

Also you can find here how PHP evaluate the booleans:

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).
like image 113
RageZ Avatar answered Jun 18 '26 23:06

RageZ



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!