Three ways to convert an array into bool type in php: 1. Add the target type enclosed in parentheses before the array variable, the syntax is "(bool)$array variable name" or "(boolean)$ Array variable name". 2. Use the boolval() function, the syntax is "boolval($array variable name)". 3. Use the settype() function to set the array variable to bool type, with the syntax "settype($array variable name, "boolean")".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php Lieutenant General Array Three methods to convert to bool type:
Method 1. Add the target type "(bool)" or "(boolean) enclosed in parentheses before the array variable ”
(bool), (boolean): Variables can be converted into Boolean types
Implementation example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(1,2,3); var_dump($arr); $bool=(bool)$arr; var_dump($bool); $arr=array(); var_dump($arr); $bool=(bool)$arr; var_dump($bool); ?>
Method 2: Use boolval() function
boolval function is used to obtain the Boolean value of a variable.
Implementation example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(1,2,3); var_dump($arr); $bool=boolval($arr); var_dump($bool); ?>
Method 3: Use settype() function
##settype( $var,$type) function can set the specified variable to the specified
$type type.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(); var_dump($arr); $a=settype($arr,"boolean"); var_dump($arr); ?>
PHP Video Tutorial"
The above is the detailed content of How to convert array to bool type in php. For more information, please follow other related articles on the PHP Chinese website!