Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

php - How to Convert Boolean to String

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simplest solution:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...