Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP boolean to string with modification & a condition

When echoing a boolean (true or false), PHP converts it to 1 or <nothing> and displays it. e.g.:

$x = true; echo $x; //displays: 1
$x = false; echo $x; //displays: <nothing>

My Question: Is there a PHP function (if not how to code it) which can display exactly "true" or "false" (and not 1 or nothing), if a variable is a boolean otherwise just display as PHP would normally display it.

like image 672
WhatIsOpenID Avatar asked Sep 08 '10 20:09

WhatIsOpenID


1 Answers

I've just been looking to do this but needed to to it inline without a function so I just used :

echo(is_bool($x) ? ($x ? "true":"false"):$x);

Not the easiest to read but gets the job done!

like image 171
Anthony Chaffey Avatar answered Oct 14 '22 20:10

Anthony Chaffey