Anonymous1
Anonymous1

Reputation: 3907

PHP - Get bool to echo false when false

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?

Upvotes: 217

Views: 226967

Answers (14)

Mohamad Hamouday
Mohamad Hamouday

Reputation: 2743

You can try this if you want debug an array:

function debug_array($a){
    return array_map(function($v){
        return is_bool($v) ? ($v ? 'true' : 'false') : $v;
    }, $a);
}

To use it:

$arr = debug_array(['test' => true, 'id' => false]);
print_r($arr); // output Array ( [test] => true [id] => false )

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78984

Your'e casting a boolean to boolean and expecting an integer to be displayed. It works for true but not false. Since you expect an integer:

echo (int)$bool_val;

Upvotes: 3

Jon Surrell
Jon Surrell

Reputation: 9637

var_export provides the desired functionality.

This will always print a value rather than printing nothing for null or false. var_export prints a PHP representation of the argument it's passed, the output could be copy/pasted back into PHP.

var_export(true);    // true
var_export(false);   // false
var_export(1);       // 1
var_export(0);       // 0
var_export(null);    // NULL
var_export('true');  // 'true'   <-- note the quotes
var_export('false'); // 'false'

If you want to print strings "true" or "false", you can cast to a boolean as below, but beware of the peculiarities:

var_export((bool) true);   // true
var_export((bool) false);  // false
var_export((bool) 1);      // true
var_export((bool) 0);      // false
var_export((bool) '');     // false
var_export((bool) 'true'); // true
var_export((bool) null);   // false

// !! CAREFUL WITH CASTING !!
var_export((bool) 'false'); // true
var_export((bool) '0');     // false

Upvotes: 26

Saad
Saad

Reputation: 13649

This is the easiest way to do this:

$text = var_export($bool_value,true);
echo $text;

or

var_export($bool_value)

If the second argument is not true, it will output the result directly.

Upvotes: 86

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798486

No, since the other option is modifying the Zend engine, and one would be hard-pressed to call that a "better way".

Edit:

If you really wanted to, you could use an array:

$boolarray = Array(false => 'false', true => 'true');
echo $boolarray[false];

Upvotes: 34

serdar.sanri
serdar.sanri

Reputation: 2228

This will print out boolean value as it is, instead of 1/0.

    $bool = false;

    echo json_encode($bool);   //false

Upvotes: 54

Harry Pehkonen
Harry Pehkonen

Reputation: 3038

json_encode will do it out-of-the-box, but it's not pretty (indented, etc):

echo json_encode(array('whatever' => TRUE, 'somethingelse' => FALSE));

...gives...

{"whatever":true,"somethingelse":false}

Upvotes: 2

David Oliver
David Oliver

Reputation: 2502

This gives 0 or 1:

intval($bool_val);

PHP Manual: intval function

Upvotes: 6

mohamedeimo
mohamedeimo

Reputation: 69

echo(var_export($var)); 

When $var is boolean variable, true or false will be printed out.

Upvotes: 6

nuala
nuala

Reputation: 2688

The %b option of sprintf() will convert a boolean to an integer:

echo sprintf("False will print as %b", false); //False will print as 0
echo sprintf("True will print as %b", true); //True will print as 1

If you're not familiar with it: You can give this function an arbitrary amount of parameters while the first one should be your ouput string spiced with replacement strings like %b or %s for general string replacement.

Each pattern will be replaced by the argument in order:

echo sprintf("<h1>%s</h1><p>%s<br/>%s</p>", "Neat Headline", "First Line in the paragraph", "My last words before this demo is over");

Upvotes: 6

akond
akond

Reputation: 16035

I like this one to print that out

var_dump ($var);

Upvotes: 23

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

You can use a ternary operator

echo false ? 'true' : 'false';

Upvotes: 3

Dan Grossman
Dan Grossman

Reputation: 52372

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

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

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

Upvotes: 303

Yanick Rochon
Yanick Rochon

Reputation: 53521

Try converting your boolean to an integer?

 echo (int)$bool_val;

Upvotes: 23

Related Questions