skip to Main Content

I’m trying to convert a bool value to a string value

function booleanToString($b) {
    // your code here
    if (gettype($b)=="boolean"){
        return (string) $b;
    }
  }
echo booleanToString(false)

I expected this code to convert bool values to string values

2

Answers


  1. I think you can use this code.

    function booleanToString($b) {
        if ($b === true) {
            return 'true';
        }
     
        if ($b === false) {
            return 'false';
        }
     
        return $b;
    }
    
    Login or Signup to reply.
  2. function boolToString(bool $value) {
      return $value ? 'true' : 'false';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search