PHP Data Type-Boolean Boolean type

Source: Internet
Author: User
Tags type null

Grammar:

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

The code is as follows Copy Code
<?php
$foo = True; Assign the value TRUE to $foo
?>

The Boolean value returned by the usual operator is passed to the control flow.

The code is as follows Copy Code

<?php
= = 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 "}

// ... Because you can use the following simple way:
if ($show _separators) {
echo "}
?>

Convert to Boolean value

To explicitly convert a value to a Boolean, use (BOOL) or (Boolean) to cast. In many cases, however, a cast is not required because the value is automatically converted when the operator, function, or process control structure requires a Boolean parameter.

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


1. Boolean value FALSE itself
2. Integer value 0 (0)
3. Floating-point value 0.0 (0)
4. Empty string, and string "0"
5. An array that does not include any elements
6. Objects that do not include any member variables (PHP 4.0 applies only)
7. Special type NULL (including variables that have not been assigned)
8. SimpleXML object generated from the empty tag
All other values are considered TRUE (including any resources).

Note: 1 and other non-0 values (both positive and negative) are considered TRUE!

  code is as follows copy code
<?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); //bool (TRUE)
Var_dump ((BOOL) array ());  &nbs p; //bool (FALSE)
Var_dump ((bool)   "false");   //bool (TRUE)
?>

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.