How to convert boolean type to string in php

青灯夜游
Release: 2023-03-13 21:22:01
Original
3118 people have browsed it

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:

'; echo '变量 $bool2 的类型为:'.gettype($bool2).'

'; $str1=strval($bool1); echo '变量 $str1 的类型为:'.gettype($str1).'
'; $str2=strval($bool2); echo '变量 $str2 的类型为:'.gettype($str2).'
'; ?>
Copy after login

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:

 原 $bool2 的类型为:'.gettype($bool2)."

"; settype($bool1, 'string'); settype($bool2, 'string'); echo '现 $bool1 的类型为:'.gettype($bool1).'
现 $bool2 的类型为:'.gettype($bool2); ?>
Copy after login
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!

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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!