为什么 PHP 不打印 TRUE/FALSE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11921690/
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
Why doesn't PHP print TRUE/FALSE?
提问by Agnel Kurian
Possible Duplicate:
PHP - Get bool to echo false when false
Given the following test.php:
鉴于以下 test.php:
<?php
echo TRUE . "\n"; // prints "1\n"
echo FALSE . "\n"; // prints "\n"
?>
Why doesn't php -f test.phpprint TRUEor FALSE? More importantly, in the FALSEcase, why doesn't it print anything?
为什么不php -f test.php打印TRUE或FALSE?更重要的是,在这种FALSE情况下,它为什么不打印任何东西?
回答by Mark Byers
回答by Peter
Because false == '';
因为 false == '';
do this to print booleans:
这样做来打印布尔值:
$bool = false;
echo $bool ? 'true' : 'false';
or...
或者...
echo $bool ? 'yes' : 'no';
echo $bool ? '1' : '0';
回答by deceze
Because boolean values when cast to a string are cast to 1and an empty string respectively.
因为布尔值在转换为字符串时分别转换为1和空字符串。
Supposedly this is to enable a transparent roundtrip between boolean -> string -> boolean.
据说这是为了启用布尔值 -> 字符串 -> 布尔值之间的透明往返。

