Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Boolean to String

Tags:

string

php

I have a Boolean variable which I want to convert to a string:

$res = true; 

I need the converted value to be of 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 that string and String are not recognized functions.
How do I convert this Boolean to a string in the format of "true" or "false" in PHP?

like image 582
tag Avatar asked May 08 '10 18:05

tag


People also ask

Can a boolean be a string?

There are two methods by which we can convert a boolean to String: 1) Method 1: Using String. valueOf(boolean b): This method accepts the boolean argument and converts it into an equivalent String value.

How do you convert a boolean to a string in Python?

Python convert boolean to string To convert boolean to string in python, we will use str(bool) and then it will be converted to string.

How do you convert boolean to string in Excel?

Convert Boolean values (TRUE or FALSE) to text in Excel For example, the original formula is =B2>C2, you can change the formula to =IF(B2>C2,"Yes","NO"). This new formula will change TRUE to Yes, and change FALSE to No.


2 Answers

Simplest solution:

$converted_res = $res ? 'true' : 'false';

like image 62
hobodave Avatar answered Oct 13 '22 10:10

hobodave


The function var_export returns a string representation of a variable, so you could do this:

var_export($res, true); 

The second argument tells the function to return the string instead of echoing it.

like image 42
Christian Davén Avatar answered Oct 13 '22 11:10

Christian Davén