How to Convert Boolean to String

后端 未结 15 807
死守一世寂寞
死守一世寂寞 2020-11-28 02:43

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\"

15条回答
  •  误落风尘
    2020-11-28 03:25

    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"
    

提交回复
热议问题