tags:

views:

166

answers:

4

I have a boolean variable which I want to convert to a string

$res = true;

I need it the converted value to also be in the format "true" "false" not "0" "1"

$converted_res = "true";
$converted_res = "false";

I've tried:

$converted_res = string($res);
$converted_res = String($res);

but it tells me string and String are not recognized functions. How do I convert this boolean to a string in the format "true" or "false" in php?

+13  A: 
$converted_res = ($res) ? 'true' : 'false';
hobodave
This is the easyest way to do it, but it depends on what you need it for it might not be the best sulution.
DoomStone
+1  A: 

You use strval() or (string) to convert to string in PHP. However, that does not convert boolean into the actual spelling of "true" or "false" so you must do that by yourself. Here's an example function:

function strbool($value)
{
    return $value ? 'true' : 'false';
}
echo strbool(false); // "false"
echo strbool(true); // "true"
treznik
-1 Neither of those are the correct answer to his question.
hobodave
If `$val = true;` then `strval($val)` and `(string) $val` both return 1.
ABach
Hang on - did you remove your incorrect answer and then copy what @hobodave wrote?
ABach
@tab used String() and string() for casting so I corrected him with the actual casting in PHP. Then I edited and offered a custom solution as well. Didn't even see what @hobodave wrote. Why the urge to scandalize? I was just trying to help :). Also, I didn't REMOVE anything.
treznik
+1 Because I get what you were saying and nobody else bothered to explain to tag why his attempts at casting were throwing errors. I mean really, downvote for a partial answer and then downvote more when it's explained further? A ternary assignment statement isn't exactly super advanced stuff, calling plagiarism on that is like complaining that someone used your brilliant idea of using a foreach loop to iterate through an array.
Syntax Error
@skidding - just to be clear, I downvoted you because your original answer was wrong (which I explained in my original comment). I was simply surprised that you then removed both strval() and (string) from your example and opted down the correct route, which had already been explained. I believe that you meant no harm, but it's important to see why we downvoted you in the first place.
ABach
@ABach - OK, let's say I understand that you downvoted me because my answer was not an actual solution. I guess I prefer the idea of discussion than bounty hunting. And again, I didn't remove anything, I just appended more info. The first sentence was the initial one and it's intact. Anyway, @tag has a lot to process now. :)
treznik
@treznik - you're right, bounty hunting is not the right route. That wasn't my intention, but I understand that it came off strange. My sincere apologies.
ABach
+6  A: 

See var_export

dev-null-dweller
Ooh, I had forgotten about this one - nice. :)
ABach
A: 
hey
@Donator: Have you ever tried to indent your source code using 4 spaces?
Andreas Rejbrand