PHP boolean data type false true usage Introduction

Source: Internet
Author: User
Tags type null

In php, false true indicates true or false. in php, it is called a boolean data type. It is a common data type. For example, conditional judgment is also a boolean data type, the following is an introduction.

Syntax

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

The Code is as follows: Copy code

<? Php
$ Foo = True; // value TRUE to $ foo
?>

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

The Code is as follows: Copy code

// = True value judgment statement
// Equality and returns a boolean
If ($ action = "show_version "){
Echo "The version is 1.0 ";
}

// This write is not required...
If ($ show_separators = TRUE ){
Echo "}

// Write directly, which is simpler
If ($ show_separators ){
Echo "}


Boolean is the simplest type in PHP. The value can be TRUE or FALSE.

For example:

The Code is as follows: Copy code

$ Foo = false;
$ Foo1 = true;
When echo "is false, the output value is:". $ foo; // No output value
Echo "<br/> true:". $ foo1; // output 1

The main details are as follows:

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

1. the Boolean value is FALSE.
2. the integer value is 0 (0)
3. the floating point value is a 0.0 (zero) null string and the string "0"
4. arrays that do not contain any elements
5. objects that do not include any member variables (applicable only to PHP 4.0)
6. Special Type NULL (including unset variables)
7. SimpleXML object generated from XML documents without any tags

The Code is as follows: Copy code

// $ A = 0;
// $ A = 0.0;
$ A = "0 ";
Var_dump (bool) 0 );
Echo "<br/> ";
Var_dump (bool) array ());
If ($ a = false ){
Echo "null 0 is converted to false by default. Success! ";
} Else {
Echo "cannot be converted to false ";
}

Output:

Bool (false)
Bool (false) null 0 is converted to false by default. Success!


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 an operator, function, or flow control requires a boolean parameter.

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

Boolean value FALSE

Integer value 0 (0)

Floating point value: 0.0 (0)

Blank string and string "0"

Array without member variables

Objects without cells (applicable only to PHP 4)

Special Type NULL (including unset variables)

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

Condition if else

If Condition Statement syntax
The If structure is the most commonly used in programming languages. The syntax is as follows:

If (expr)
Statement
Expr indicates the condition, and statement indicates the statement to be executed after the condition is met. This means that if a condition (expr) is met, the following statement (statement) will be executed. If the condition is not met, nothing will be done.

Example:

The Code is as follows: Copy code
<? Phpif ($ a> $ B) echo "a is bigger than B";?>

The preceding example indicates that if the variable $ a> $ B, "a is bigger than B" is output. Otherwise, nothing is done.

After the if condition is met, if the statement to be executed has more than one row, enclose the multi-row statement with braces. Example:

The Code is as follows: Copy code
<? Phpif ($ a> $ B) {echo "a is bigger than B"; $ B = $ a ;}?>

In this example, if the variable $ a> $ B, output "a is bigger than B" and assign $ a to $ B.

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.