English 中文(简体)
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.

最佳回答

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).
问题回答

No casting happens in your example.
Your problem is likely somewhere else.

Would you mind to share a more complete piece of code?

Try:


if((bool)$result["Users"]["is_login"] == true)
{
    // do something
}

.

And reply to one of your encounter:

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

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

since your return value is boolean value true, in PHP it doesn t have a representation for it, therefore appear as empty (mainwhile, if the value is a boolean false, then you ll see a 0





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

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 ...

热门标签