Home > Backend Development > PHP Tutorial > php Boolean type (Boolean)

php Boolean type (Boolean)

伊谢尔伦
Release: 2016-11-24 14:07:24
Original
1415 people have browsed it

This is the simplest type. boolean expresses a truth value and can be TRUE or FALSE.

Syntax

To specify a boolean value, use the keywords TRUE or FALSE. Both are not case sensitive.

<?php
$foo = True; // assign the value TRUE to $foo
?>
通常运算符所返回的 boolean 值结果会被传递给控制流程。
<?php
// == 是一个操作符,它检测两个变量是否相等,并返回一个布尔值
if ($action == "show_version") {
   echo "The version is 1.23";
}
// 这样做是不必要的...
if ($show_separators == TRUE) {
   echo "<hr>\n";
}
// ...因为可以使用下面这种简单的方式:
if ($show_separators) {
   echo "<hr>\n";
}
?>
Copy after login

Convert to Boolean

To explicitly convert a value to boolean, use (bool) or (boolean) to cast. But in many cases, casting is not necessary because when an operator, function, or flow control structure requires a boolean parameter, the value is automatically converted.

See the discrimination of type conversion.

When converted to boolean, the following values ​​are considered FALSE:

Boolean value FALSE itself

Integer value 0 (zero)

Floating point value 0.0 (zero)

The empty string, and the string " 0"

An array that does not include any elements

An object that does not include any member variables (only applicable to PHP 4.0)

Special type NULL (including variables that have not been assigned a value)

Generated from an XML document without any tags (tags) SimpleXML objects

All other values ​​are considered TRUE (including any resources).

Warning

-1, like other non-zero values ​​(positive or negative), is considered TRUE!

<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)
?>
Copy after login


Related labels:
php
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