PHP - 假时获取布尔以回显假

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

以下代码不打印任何内容:

The following code doesn't print out anything:

$bool_val = (bool)false;
echo $bool_val;

但是下面的代码打印出 1:

But the following code prints 1:

$bool_val = (bool)true;
echo $bool_val;

$bool_valfalse 时,有没有比添加 0false 更好的方法来打印code>if 语句?

Is there a better way to print 0 or false when $bool_val is false than adding an if statement?

推荐答案

echo $bool_val ? 'true' : 'false';

或者如果你只想在它为假时输出:

Or if you only want output when it's false:

echo !$bool_val ? 'false' : '';

相关文章