PHP打印的布尔值是空的,为什么?

2022-01-19 00:00:00 boolean php echo

我是 PHP 新手.我正在实现一个脚本,但我对以下内容感到困惑:

I am new to PHP. I am implementing a script and I am puzzled by the following:

$local_rate_filename = $_SERVER['DOCUMENT_ROOT']."/ghjr324l.txt";
$local_rates_file_exists = file_exists($local_rate_filename);

echo $local_rates_file_exists."<br>";

这段代码显示一个空字符串,而不是 0 或 1(或 true 或 false).为什么?文档似乎表明布尔值始终为 0 或 1.这背后的逻辑是什么?

This piece of code displays an empty string, rather than 0 or 1 (or true or false). Why? Documentation seems to indicate that a boolean value is always 0 or 1. What is the logic behind this?

推荐答案

使用布尔值来回转换时要小心,手册说:

Be careful when you convert back and forth with boolean, the manual says:

布尔值 TRUE 转换为字符串1".布尔值 FALSE 是转换为"(空字符串).这允许转换回来和介于布尔值和字符串值之间.

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.

所以你需要做一个:

echo (int)$local_rates_file_exists."<br>";

相关文章