PHP 中的 true/false 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2382490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does true/false work in PHP?
提问by Moon
I wonder how PHP handles true/falsecomparison internally.
I understand that true is defined as 1 and false is defined as 0.
When I do if("a"){ echo "true";}it echos "true". How does PHP recognize "a" as 1 ?
我想知道 PHP 如何在内部处理真/假比较。我知道 true 定义为 1,false 定义为 0。当我这样做时,if("a"){ echo "true";}它会回显“ true”。PHP 如何将 "a" 识别为 1 ?
回答by Mark Byers
This is covered in the PHP documentation for booleansand type comparison tables.
When converting to boolean, the following values are considered FALSE:
转换为布尔值时,以下值被视为 FALSE:
- the boolean
FALSEitself - 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
- 布尔值
FALSE本身 - 整数
0(零) - 浮点数
0.0(零) - 空字符串和字符串
'0' - 一个元素为零的数组
- 具有零成员变量的对象(仅限 PHP 4)
- 特殊类型
NULL(包括未设置的变量) - 从空标签创建的 SimpleXML 对象
Every other value is considered TRUE.
其他所有值都被视为 TRUE。
回答by Byron Whitlock
回答by akinuri
Since I've visited this page several times, I've decided to post an example (loose) comparison test.
由于我已多次访问此页面,因此我决定发布一个示例(松散的)比较测试。
Results:
结果:
"" -> false
"0" -> false
"1" -> true
"01" -> true
"abc" -> true
"true" -> true
"false" -> true
0 -> false
0.1 -> true
1 -> true
1.1 -> true
-42 -> true
"NAN" -> true
0 -> false
-> true
null -> false
true -> true
false -> false
[] -> false
["a"] -> true
{} -> true
{} -> true
{"s":"f"} -> true
Code:
代码:
class Vegetable {}
class Fruit {
public $s = "f";
}
$cases = [
"",
"0",
"1",
"01",
"abc",
"true",
"false",
0,
0.1,
1,
1.1,
-42,
"NAN",
(float) "NAN",
NAN,
null,
true,
false,
[],
["a"],
new stdClass(),
new Vegetable(),
new Fruit(),
];
echo "<pre>" . PHP_EOL;
foreach ($cases as $case) {
printf("%s -> %s" . PHP_EOL, str_pad(json_encode($case), 9, " ", STR_PAD_RIGHT), json_encode( $case == true ));
}
When a strict (===) comparison is done, everything except truereturns false.
===完成严格 ( ) 比较后,除true返回之外的所有内容都返回false。
回答by Citizen
The best operator for strict checking is
严格检查的最佳运算符是
if($foo === true){}
That way, you're really checking if its true, and not 1 or simply just set.
这样,您实际上是在检查它是否为真,而不是 1 或只是设置。
回答by sidereal
PHP uses weak typing (which it calls 'type juggling'), which is a bad idea (though that's a conversation for another time). When you try to use a variable in a context that requires a boolean, it will convert whatever your variable is into a boolean, according to some mostly arbitrary rules available here: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
PHP 使用弱类型(它称之为“类型杂耍”),这是一个坏主意(尽管这是另一个话题)。当您尝试在需要布尔值的上下文中使用变量时,它会将您的任何变量转换为布尔值,根据此处提供的一些大多数任意规则:http: //www.php.net/manual/en/language .types.boolean.php#language.types.boolean.casting
回答by Anycorn
think of operator as unary function: is_false(type value)which returns true or false, depending on the exact implementation for specific type and value. Consider if statement to invoke such function implicitly, via syntactic sugar.
将运算符视为一元函数:is_false(type value)返回 true 或 false,具体取决于特定类型和值的确切实现。考虑通过语法糖隐式调用此类函数的 if 语句。
other possibility is that type has cast operator, which turns type into another type implicitly, in this case string to Boolean.
另一种可能性是类型具有强制转换运算符,它隐式地将类型转换为另一种类型,在这种情况下是字符串到布尔值。
PHP does not expose such details, but C++ allows operator overloading which exposes fine details of operator implementation.
PHP 不会公开此类细节,但 C++ 允许运算符重载,从而公开运算符实现的精细细节。

