views:

121

answers:

3

When I cast to Boolean (using (bool)), is there a built in way to get PHP to actually return the constants true or false. At the moment I'm getting 1 or blank, which evaluate to true and false respectively.

I want the value returned for clearer semantics. However, if I can't get it, I'll just settle with 1 and blank.

Update

I've realised this question is a bit... stupid. I mean even if I return true or false, they'll evaluate to 1 or blank anyway. I definitely do not want strings! :)

Sorry to waste anyone's time.

+1  A: 

PHP displays boolean values as 1 (true) or empty string (false) when outputted.

If you want to check if it's true or false use == (if implicit conversion is OK) or === (if it's not). For example:

echo $val ? 'true' : 'false'; // implicit conversion
echo $val === true ? 'true' : 'false'; // no conversion

I don't know of any way to make PHP output boolean values natively as true or false.

cletus
I think he's wanting the string values "true" or "false".
fiXedd
Thanks, but that didn't tell me anything I didn't already know. I think my question is a bit silly actually.
alex
@fiXedd: my last sentence doesn't address that?
cletus
A: 

If you're looking for the strings "true" and "false," a ternary conditional would be perfect:

<?=(($boolean) ? "true" : "false")?>
Dolph
Thanks, this would be a good solution. However, it is not what I'm chasing.
alex
A: 

In case you're too lazy to do a comparaison and echo a string or if you just want to keep it short you can use :

var_export($boolean, true); // the second parameter is to return and not output

PHP: var_export

kevin