为什么 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:27:08  来源:igfitidea点击:

Why doesn't PHP print TRUE/FALSE?

php

提问by Agnel Kurian

Possible Duplicate:
PHP - Get bool to echo false when false

可能的重复:
PHP - 获取 bool 以在 false 时回显 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打印TRUEFALSE?更重要的是,在这种FALSE情况下,它为什么不打印任何东西?

回答by Mark Byers

From the manual:

手册

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

布尔值 TRUE 被转换为字符串“1”。Boolean FALSE 转换为 ""(空字符串)。这允许在布尔值和字符串值之间来回转换。

回答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.

据说这是为了启用布尔值 -> 字符串 -> 布尔值之间的透明往返。