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

How to convert string to boolean type in php

青灯夜游
青灯夜游Original
2021-05-27 18:27:212884browse

Method: 1. Add "(bool)" or "(boolean)" before the variable to be converted to force conversion to boolean type; 2. Use the boolval() function with the syntax "boolval(value)" ;3. Use the settype() function, the syntax is "settype(value, "boolean")".

How to convert string to boolean type in php

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

1. Forced type conversion-- Prepend the target type enclosed in parentheses (bool) or (boolean)

before the variable to be converted. Example:

<?php
$bool1 = (bool) &#39;ciao&#39;;
$bool2 = (boolean) &#39;0&#39;;
var_dump($bool1); 
var_dump($bool2); 
?>

Rendering:

How to convert string to boolean type in php

2. Use boolval() function

boolval function is used to obtain The Boolean value of the variable.

Example:

<?php
echo &#39;0:        &#39;.(boolval(0) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;42:       &#39;.(boolval(42) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;0.0:      &#39;.(boolval(0.0) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;4.2:      &#39;.(boolval(4.2) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;"":       &#39;.(boolval("") ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;"string": &#39;.(boolval("string") ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;"0":      &#39;.(boolval("0") ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;"1":      &#39;.(boolval("1") ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;[1, 2]:   &#39;.(boolval([1, 2]) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;[]:       &#39;.(boolval([]) ? &#39;true&#39; : &#39;false&#39;)."\n";
echo &#39;stdClass: &#39;.(boolval(new stdClass) ? &#39;true&#39; : &#39;false&#39;)."\n";
?>

Output:

0:        false
42:       true
0.0:      false
4.2:      true
"":       false
"string": true
"0":      false
"1":      true
[1, 2]:   true
[]:       false
stdClass: true

3. Use the general type conversion function settype(mixed var,string type)

<?php  
$str="123.9sdc";  
$int=settype($str,"boolean");  
var_dump($int);  
var_dump($str); 
echo "<hr>";
$str="";  
$int=settype($str,"boolean");  
var_dump($int);  
var_dump($str); 
?>

Output:

How to convert string to boolean type in php

The settype() function is used to set the type of a variable.

Syntax

bool settype ( mixed &$var , string $type )

Set the type of variable var to type.

To be converted Variables. The possible values ​​of Recommended study: "
Parameters Description
##var
type
##type

are:

"boolean" (or "bool" since PHP 4.2.0)
  • "integer" (or "int" since PHP 4.2.0)
  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
  • "string"
  • "array"
  • "object"
  • "null" (from PHP 4.2.0)
PHP video tutorial

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