如何将布尔值转换为字符串 [英] How to Convert Boolean to String

查看:1025
本文介绍了如何将布尔值转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔变量,我想将其转换为字符串:

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

$res = true;

我需要转换后的值的格式为:"true" "false",而不是"0" "1"

I need the converted value to be of the format: "true" "false", not "0" "1"

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

我尝试过:

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

但是它告诉我stringString不是可识别的函数.
如何在PHP中将此布尔值转换为"true""false"格式的字符串?

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?

推荐答案

最简单的解决方案:

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

这篇关于如何将布尔值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆