zer*_*uan 2 php variables types
php如何转换布尔变量?
我试图将一个布尔值保存到数组:
$result["Users"]["is_login"] = true;
Run Code Online (Sandbox Code Playgroud)
但是当我使用debug时,is_login值为空.当我做条件时:
if($result["Users"]["is_login"])
Run Code Online (Sandbox Code Playgroud)
条件总是错误的.
然后我尝试这样做:
$result["Users"]["is_login"] = "true";
Run Code Online (Sandbox Code Playgroud)
它起作用了.
这并不是什么大不了的事,但是当我从函数返回布尔值时,我仍然需要将它们转换为字符串.
没有演员
该
if($result["Users"]["is_login"])
Run Code Online (Sandbox Code Playgroud)
应该管用.你可以尝试使用var_dump($result["Users"]["is_login"]);以确保变量已正确设置.
您可以使用isset(手动)功能检查是否设置了变量.
你也可以在这里找到PHP如何评估布尔值:
Run Code Online (Sandbox Code Playgroud)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).