PHP - Get bool to echo false when false

Anonymous1 picture Anonymous1 · Feb 9, 2011 · Viewed 157.3k times · Source

The following code doesn't print out anything:

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

But the following code prints 1:

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

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

Answer

Dan Grossman picture Dan Grossman · Feb 9, 2011
echo $bool_val ? 'true' : 'false';

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

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