How to convert array to bool type in php

青灯夜游
Release: 2023-03-16 12:44:01
Original
2011 people have browsed it

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")".

How to convert array to bool type in php

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

How to convert array to bool type in php

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

How to convert array to bool type in php

Method 3: Use settype() function

##settype( $var,$type) function can set the specified variable to the specified $type type.

If you want to convert the array to bool type, just set $type to "boolean" or ""bool".

Note: The settype() function will change the original array. If set Returns TRUE when successful and FALSE when failed.

Implementation example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array();
var_dump($arr);
$a=settype($arr,"boolean");
var_dump($arr);
?>
Copy after login

How to convert array to bool type in php

Recommended learning: "

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template