gpt4 book ai didi

php - php如何转换 bool 变量?

转载 作者:行者123 更新时间:2023-12-04 21:15:08 24 4
gpt4 key购买 nike

php如何转换 bool 变量?

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

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

但是当我使用调试时 is_login 值是空白的。当我做这样的条件时:

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

条件总是错误的。

然后我尝试这样做:

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

它成功了。

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

最佳答案

没有类型转换

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

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

您可以使用 isset ( manual ) 函数检查是否设置了变量。

您还可以找到here PHP 如何评估 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).

关于php - php如何转换 bool 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1726937/

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