Conversion of php Boolean data types and process-based data types

Source: Internet
Author: User
Tags type null
Php Boolean data type and process-controlled data type conversion Boolean data type is the simplest data type. It can be TRUE or FALSE, and keywords are case-insensitive. To explicitly convert an & #20540; to a boolean type, use (bool) or (boolean) to force the conversion. Php performs automatic type conversion in many cases, especially when calculating conditional expressions of flow control statements. When converted to Boolean & #20540;, the following & #20540; is considered to be the f php Boolean data type and the data type conversion controlled by the process

Boolean is the simplest data type. It can be TRUE or FALSE, and keywords are case-insensitive.

To explicitly convert a value to a boolean type, use (bool) or (boolean) to force the conversion. Php performs automatic type conversion in many cases, especially when calculating conditional expressions of flow control statements.

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 (zero );
4) blank string and string "0 ";
5) arrays without member variables;
6) objects without cells (applicable only to PHP4 );
7) Special type NULL (including unset variables );

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

You can use the following program example for testing:

 

During development, you must pay attention to the hidden danger of default conversion of Boolean values when determining conditions.

The following questions are about this:

 "; 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 (12) echo" true "; elseecho" false-10 "."
"; If (array () echo" true "; elseecho" false-11 "."
";/* Output result: false-1false-2false-3true false-5false-6true false-8false-9true false-11 analysis: if statements return false when the condition does not meet (that is, the conditional expression evaluates to false. Php's if statement does not have then and has elseif (the same as else if). when it is converted to boolean, the following values are considered false: 1) boolean values false; 2) integer value 0 (0); 3) floating point value 0.0 (0); 4) blank string and string "0"; 5) array without member variables; 6) objects without cells (applicable only to PHP4); 7) special types of NULL (including unset variables); */?>



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.