How to Convert Boolean to String

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

    function ToStr($Val=null,$T=0){
    
        return is_string($Val)?"$Val"
        :
        (
            is_numeric($Val)?($T?"$Val":$Val)
            :
            (
                is_null($Val)?"NULL"
                :
                (
                    is_bool($Val)?($Val?"TRUE":"FALSE")
                    :
                    (
                        is_array($Val)?@StrArr($Val,$T)
                        :
                        false
                    )
                )
            )
        );
    
    }
    function StrArr($Arr,$T=0)
    {
        $Str="";
        $i=-1;
        if(is_array($Arr))
        foreach($Arr AS $K => $V)
        $Str.=((++$i)?", ":null).(is_string($K)?"\"$K\"":$K)." => ".(is_string($V)?"\"$V\"":@ToStr($V,$T+1));
        return "array( ".($i?@ToStr($Arr):$Str)." )".($T?null:";");
    }
    
    $A = array(1,2,array('a'=>'b'),array('a','b','c'),true,false,ToStr(100));
    echo StrArr($A); // OR ToStr($A) // OR ToStr(true) // OR StrArr(true)
    

提交回复
热议问题