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

How to convert boolean to string in php

青灯夜游
Release: 2023-03-13 07:52:02
Original
4071 people have browsed it

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;;
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template