Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't PHP print TRUE/FALSE? [duplicate]

Tags:

php

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

Given the following test.php:

<?php

echo TRUE . "\n";    // prints "1\n"
echo FALSE . "\n";   // prints "\n"

?>

Why doesn't php -f test.php print TRUE or FALSE? More importantly, in the FALSE case, why doesn't it print anything?

like image 633
Agnel Kurian Avatar asked Aug 12 '12 11:08

Agnel Kurian


2 Answers

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.

like image 148
Mark Byers Avatar answered Oct 19 '22 08:10

Mark Byers


Because false == '';

do this to print booleans:

$bool = false;
echo $bool ? 'true' : 'false';

or...

echo $bool ? 'yes' : 'no';
echo $bool ? '1' : '0';
like image 43
Peter Avatar answered Oct 19 '22 08:10

Peter