PHP Boolean type

Source: Internet
Author: User
Tags type null
Boolean type

This is the simplest type. Boolean expresses the truth value, which can be TRUE or FALSE.

Note: The Boolean type is introduced in PHP 4.

Grammar

To specify a Boolean value, use the keyword TRUE or FALSE. Two are not case-sensitive.

$foo = True; Assign the value TRUE to $foo
?>

Typically you return a Boolean value with some operators and pass it to the control flow.

= = is an operator that detects whether two variables are equal and returns a Boolean value
if ($action = = "Show_version") {
echo "The version is 1.23";
}
It is not necessary to do so ...
if ($show _separators = = TRUE) {
echo "\ n";
}
// ... Because you can use the following simple way:
if ($show _separators) {
echo "\ n";
}
?>

Convert to a Boolean value

To explicitly convert a value to a Boolean, use either (BOOL) or (Boolean) to cast. However, there are many cases where casting is not required because the value is automatically converted when an operator, function, or process control structure requires a Boolean parameter.

See also type-trick.

When converted to Boolean, the following values are considered FALSE:

The Boolean value FALSE itself the integer value 0 (0) The Float value 0.0 (0) The empty string, and the string "0" does not include any element of an array that does not include any member variables of the object (PHP 4.0 applies only) special type NULL (including the variable that has not been set SimpleXML objects generated from XML documents without any tags (tags)

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

Warning

-1 and other non-0 values (either positive or negative) are considered to be true!

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)
?>

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.