Home >Backend Development >PHP Problem >How to convert boolean type to string in php
php method to convert Boolean type to string: 1. Use strval() function, syntax "strval($bool)"; 2. Use settype() function, syntax "settype($bool, ' string')".
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 '变量 $bool1 的类型为:'.gettype($bool1).'<br>'; echo '变量 $bool2 的类型为:'.gettype($bool2).'<br><br>'; $str1=strval($bool1); echo '变量 $str1 的类型为:'.gettype($str1).'<br>'; $str2=strval($bool2); echo '变量 $str2 的类型为:'.gettype($str2).'<br>'; ?>
Rendering:
##Method 2: Use the settype() function
settype() function: used to set the type of variables. The type that can be set is:<?php header("Content-type:text/html;charset=utf-8"); $bool1=true; $bool2=false; echo '原 $bool1 的类型为:'.gettype($bool1).'<br> 原 $bool2 的类型为:'.gettype($bool2)."<br><br>"; settype($bool1, 'string'); settype($bool2, 'string'); echo '现 $bool1 的类型为:'.gettype($bool1).'<br> 现 $bool2 的类型为:'.gettype($bool2); ?>Rendering: 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!