Home >Backend Development >PHP Problem >How to convert boolean to string in php

How to convert boolean to string in php

青灯夜游
青灯夜游Original
2021-09-30 17:19:194061browse

php method to convert boolean to string: 1. Add the target type "(string)" enclosed in parentheses before the boolean type variable, the syntax is "(string)$bool"; 2. Use strval() function, syntax "strval($bool)".

How to convert boolean to string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you want to There are many ways to convert boolean type to string type. Here are two methods:

  • Add the target type enclosed in parentheses before the variable (string )

  • Use strval() function

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=(string)$bool1;
echo &#39;变量 $str1 的类型为:&#39;.gettype($str1).&#39;<br>&#39;;
$str2=strval($bool2);
echo &#39;变量 $str2 的类型为:&#39;.gettype($str2).&#39;<br>&#39;;
?>

Output result:

How to convert boolean to string in php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert boolean 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