Home  >  Article  >  Backend Development  >  How to convert boolean type to string in php

How to convert boolean type to string in php

青灯夜游
青灯夜游Original
2021-11-17 17:58:533188browse

php method to convert Boolean type to string: 1. Use strval() function, syntax "strval($bool)"; 2. Use settype() function, syntax "settype($bool, ' string')".

How to convert boolean type to string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php will use Boolean type Convert to string

#Method 1: Use strval() function

strval() function: used to obtain the string value of a variable.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$bool1=true;
$bool2=false;
echo &#39;变量 $bool1 的类型为:&#39;.gettype($bool1).&#39;<br>&#39;;
echo &#39;变量 $bool2 的类型为:&#39;.gettype($bool2).&#39;<br><br>&#39;;
$str1=strval($bool1);
echo &#39;变量 $str1 的类型为:&#39;.gettype($str1).&#39;<br>&#39;;
$str2=strval($bool2);
echo &#39;变量 $str2 的类型为:&#39;.gettype($str2).&#39;<br>&#39;;
?>

Rendering:

How to convert boolean type to string in php

##Method 2: Use the settype() function

settype() function: used to set the type of variables.

The type that can be set is:

  • "boolean" (or "bool", starting from PHP 4.2.0)

  • "integer" (or "int" since PHP 4.2.0)

  • "float" (only available since PHP 4.2.0, for older versions "double" used in is now deprecated)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

Note: The settype() function will change the variable type itself.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$bool1=true;
$bool2=false;
echo &#39;原 $bool1 的类型为:&#39;.gettype($bool1).&#39;<br> 原 $bool2 的类型为:&#39;.gettype($bool2)."<br><br>";
settype($bool1, &#39;string&#39;);
settype($bool2, &#39;string&#39;);
echo &#39;现 $bool1 的类型为:&#39;.gettype($bool1).&#39;<br> 现 $bool2 的类型为:&#39;.gettype($bool2);

?>

Rendering:

How to convert boolean type to string in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to convert boolean type to string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn