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
(bool)
cast is redundant, you don't need to cast bool to bool:(bool) true === true
. – Jon Surrellecho 0==1;
outputs0
orfalse
? – mario