Home > Article > Backend Development > What is the php string boolean type?
php String Boolean type refers to the Boolean Boolean type. bool has only two values, which are used to express true values, either true or false. To specify a Boolean type, you can use the constant true or false, and its setting The syntax is "$foo = True;", which means setting "$foo" to "TRUE".
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
What is the Boolean type of php string ?
Boolean Boolean type
bool has only two values, used to express the truth value, either true or false.
Syntax
To specify a bool, use the constant true or false. Both are not case sensitive.
<?php $foo = True; // 设置 $foo 为 TRUE ?>
Normally the bool value returned by the operator will be passed to the control flow.
<?php // == 是一个操作符,它检测两个变量是否相等,并返回一个布尔值 if ($action == "show_version") { echo "The version is 1.23"; } // 这样做是不必要的... if ($show_separators == TRUE) { echo "<hr>\n"; } // ...因为可以使用下面这种简单的方式: if ($show_separators) { echo "<hr>\n"; } ?>
Convert to Boolean
To explicitly convert a value to bool, you can use (bool) cast. Normally this is not necessary because the value will automatically be interpreted as a bool value when used in a logical context. Please read the Type Conversion page for more information.
See the identification of type conversion.
When converted to bool, the following values are considered false:
The Boolean value false itself
Integer value 0 (zero)
Floating point value 0.0 (zero)-0.0 (zero)
Empty string "", and string "0"
An array that does not include any elements
Unit type NULL (including variables that have not been assigned a value)
The coercion behavior of internal objects is overloaded as bool. For example: a SimpleXML object created from an empty element with no attributes.
All other values are considered true (including resources and NAN).
Warning:
-1, like other non-zero values (positive or negative), is considered true!
<?php var_dump((bool) ""); // bool(false) var_dump((bool) "0"); // 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) ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the php string boolean type?. For more information, please follow other related articles on the PHP Chinese website!