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

标签 php boolean echo

我是 PHP 新手。我正在执行一个脚本,我对以下内容感到困惑:

$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)。为什么?文档似乎表明 boolean 值始终为 0 或 1。这背后的逻辑是什么?

最佳答案

使用 boolean 值来回转换时要小心,the manual says :

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>";

关于PHP 打印 boolean 值是空的,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9042002/

相关文章:

echo - 在 Katalon Recorder 中使用 storeValue 和 echo

javascript - 如何在用户单击后退按钮或离开时执行功能。以及如何延迟它?

php - WordPress 通知 : Trying to get property of non-object

php - 无法使用HTML读取网页上的Mysql数据

c++ - 我可以将 int 与 C++ 中的 boolean 值相乘吗?

reactjs - 禁用和启用按钮的样式

php - 如何计算 PHP 数组中的非空条目?

java - 如何输出我存储的这些信息?

php - 我的 echo 语句不会显示在 html 行中

PHP 根据工作日查找日期