Home >Backend Development >PHP Problem >How to convert value to bool in php

How to convert value to bool in php

zbt
zbtOriginal
2023-06-13 10:52:431588browse

php method to convert value to bool: 1. Convert other data types to bool type; 2. Convert bool type to other data types, and convert true to integer, string or floating point type; 3. Use built-in functions for type conversion, such as intval() function, floatval() function, strval() function and settype() function to achieve data type conversion.

How to convert value to bool in php

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

PHP is an untyped language, which can change the type of variables at will during execution. One of the basic data types is Boolean. The Boolean type has only two values, true and false (true and false).

In some cases, it is necessary to convert bool type variables to other data types, or to convert other data types to bool type. in PHP , these conversions can be accomplished by casting or using built-in functions.

1. Convert other data types to bool type

In PHP, you can use the following rules to convert other data types to bool type:

If the variable is an integer or floating point type and the value is 0 or 0.0, it is converted to false, otherwise it is true. If the variable is of string type and the value is an empty string (""), it is converted to false, otherwise true. If the variable is an array type and has no members, that is, an empty array, it is converted to false, otherwise it is true. Converts to true if the variable is of type object. if the variable is NULL type, converted to false.

The following is a PHP code example:

$var1 = 0;
$var2 = 1.23;
$var3 = " ";
$var4 = "string";
$var5 = array();
$var6 = new stdClass();
$var7 = NULL;
var_dump((bool)$var1); // false
var_dump((bool)$var2); // true
var_dump((bool)$var3); // false
var_dump((bool)$var4); // true
var_dump((bool)$var5); // false
var_dump((bool)$var6); // true
var_dump((bool)$var7); // false

2. Convert bool type to other data types

In PHP, you can use the following rules to convert Convert bool type to other data types:

Convert true to integer type 1 and false to integer type 0. Convert true to the string type "1" and false to the empty string "". will be true Converts to float 1.0, false to float 0.0.

The following is a PHP code example:

$bool1 = true;
$bool2 = false;
echo (int)$bool1; // 1
echo (int)$bool2; // 0
echo (string)$bool1; // "1"
echo (string)$bool2; // ""
echo (float)$bool1; // 1.0
echo (float)$bool2; // 0.0

In addition to cast, you can also use built-in functions for type conversion.

3. Use built-in functions for type conversion

intval() function

intval() function can convert a string into an integer value. If the string starts with a number, it is converted directly to an integer, otherwise 0 is returned.

$str = "123.45abc";
echo intval($str); // 123

floatval() function

floatval() function can convert a string to a floating point value.

$str = "123.45abc";
echo floatval($str); // 123.45

strval() function

strval() function can convert a value to a string type.

$val = 12345;
echo strval($val); // "12345"

settype() function

settype() function can convert a variable to a specified type. The first parameter of this function is the variable to be converted, and the second parameter is the type to be converted.

$str = "123.45abc";
settype($str, "float");
echo $str; // 123.45

To sum up, it is very simple to implement data type conversion in PHP. Proficient in PHP type conversion techniques can improve the readability and expressiveness of your code.

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