PHP Boolean type

Source: Internet
Author: User
PHP Boolean type

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

Note: The Boolean type is introduced in PHP 4.

Syntax

To specify a Boolean value, use the keyword TRUE or FALSE. Both are case insensitive.

$ Foo = True; // assign the value TRUE to $ foo
?>

You usually use some operators to return a boolean value and pass it to the control process.

// = Is an operator that checks whether two variables are equal and returns a boolean value.
If ($ action = "show_version "){
Echo "The version is 1.23 ";
}
// This is not necessary...
If ($ show_separators = TRUE ){
Echo "\ n ";
}
//... Because the following simple method can be used:
If ($ show_separators ){
Echo "\ n ";
}
?>

Convert to a Boolean value

To convert a value to a boolean value explicitly, use (bool) or (boolean) to forcibly convert the value. However, in many cases, forced conversion is not required, because this value is automatically converted when the operator, function, or flow control structure requires a boolean parameter.

See type tricks.

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

The Boolean value FALSE itself the integer value 0 (0) the floating point value 0.0 (0) null string, and the string "0" does not include any element array does not include any member variable object (applicable only in PHP 4.0) Special type NULL (including unset variables) simpleXML object generated from an XML document without any tags

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

Warning

-1 is considered TRUE like other non-zero values (both positive and negative values!

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

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.