-1

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

  • Are you looking for this ? https://onlinephp.io/c/6732f – executable Mar 20 '23 at 08:30
  • [A bool true value is converted to the string "1". bool false is converted to "" (the empty string). This allows conversion back and forth between bool and string values.](https://www.php.net/en/language.types.string) – shingo Mar 20 '23 at 08:31

2 Answers2

1
function boolToString(bool $value) {
  return $value ? 'true' : 'false';
}
0

I think you can use this code.

function booleanToString($b) {
    if ($b === true) {
        return 'true';
    }
 
    if ($b === false) {
        return 'false';
    }
 
    return $b;
}
Wang YinXing
  • 278
  • 1
  • 6