How to Convert Boolean to String

后端 未结 15 802
死守一世寂寞
死守一世寂寞 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:12

    This works also for any kind of value:

    $a = true;
    
    echo $a                     // outputs:   1
    echo value_To_String( $a )  // outputs:   true
    

    code:

    function valueToString( $value ){ 
        return ( !is_bool( $value ) ?  $value : ($value ? 'true' : 'false' )  ); 
    }
    

提交回复
热议问题