php 如何转换布尔变量? [英] How does php cast boolean variables?

查看:31
本文介绍了php 如何转换布尔变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php 如何转换布尔变量?

我试图将一个布尔值保存到一个数组中:

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

但是当我使用 debug 时,is_login 值为空.当我做这样的条件时:

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

条件总是假的.

然后我尝试这样做:

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

它奏效了.

这没什么大不了的,但是当我从函数返回布尔值时,我仍然需要将它们转换为字符串.

解决方案

没有演员表

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

应该可以.你可以尝试使用 var_dump($result["Users"]["is_login"]); 来确保变量设置正确.

您可以使用isset(手册) 函数.

您也可以在这里找到 PHP 如何评估布尔值:

<块引用>

转换为布尔值时,以下值被视为 FALSE:布尔值 FALSE 本身整数 0(零)浮点数 0.0(零)空字符串和字符串0"一个元素为零的数组具有零成员变量的对象(仅限 PHP 4)特殊类型 NULL(包括未设置的变量)从空标签创建的 SimpleXML 对象每隔一个值都被认为是 TRUE(包括任何资源).

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

这篇关于php 如何转换布尔变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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