PHP Boolean data types and process-controlled data type conversions

Source: Internet
Author: User
Tags type null
PHP Boolean data types and process-controlled data type conversions

The Boolean is the simplest type of data. can be true or false, and the keyword is not case-sensitive.

To explicitly convert a value to a Boolean type, use (bool) or (Boolean) to cast. In many cases, PHP will perform automatic type conversions, especially when the conditional expression of a Process Control statement is evaluated.

When converted to a Boolean value, the following values are considered false:

1) boolean value false;
2) integer value 0 (0);
3) floating-point value 0.0 (0);
4) Blank string and string "0";
5) No array of member variables;
6) object without unit (only applicable to PHP4);
7) Special type null (including variables not yet set);

In addition, all other values are considered true (including any resources).

The following program examples can be used to test:

 
  

During the development process, when the condition is judged, it is important to pay attention to the recessive danger of the Boolean type default conversion.

The following topics are in this regard:

 
  "; if (" 0 ") echo" true "; Elseecho" False-2 "."
"; if ($i) echo" true "; Elseecho" false-3 "."
"; if (" false ") echo" true "; Elseecho" false-4 "."
"; if (false) echo" true "; Elseecho" false-5 "."
"; if (0.0) echo" true "; Elseecho" false-6 "."
"; if (PHP) echo" true "; Elseecho" false-7 "."
"; if (") echo "true"; Elseecho "False-8". "
"; if (null) echo" true "; Elseecho" false-9 "."
"; if (array) echo" true "; Elseecho" false-10 "."
";" if (Array ()) echo "true"; Elseecho "false-11". "
";/* Output: False-1false-2false-3true false-5false-6true false-8false-9true FALSE-11 Analysis: If statement returns false when the condition is not satisfied (that is, the conditional expression evaluates to false). PHP If statement does not have then, there is elseif (same else if) when converted to Boolean, the following values are considered False:1) boolean value false;2) integer value 0 (0); 3) floating-point value 0.0 (0); 4) blank string and string "0" ; 5) No array of member variables, 6) object with no cell (only for PHP4), 7) special type null (including variable not yet set);*/?>



  • 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.