PHP operator (v) "logical operator" Instance explained

Source: Internet
Author: User
Tags php operator
Today we are going to show you the fifth operator of PHP, which is also a very important set of operators in our program, " logical Operators”。

What is a logical operator?

Logical operation, everyone should be not unfamiliar, in the school math textbook There is a knowledge of logical operations, is what we often say "or and not"; A logical operator is a set of very important operators in a PHP program that combine the results of a logical operation.

The logical operators in PHP are the following table


Operator name Example Results
&& OR and (logical AND) $m and $n or $m && $n Returns true if both $m and $n are true, otherwise false

|| Or or (logical OR)

$m | | $n or $m or $n $m and $n at least one is true, returns true, otherwise false
XOR (Logical XOR) $m XOR $n Returns True if $m and $n have and only one is true, otherwise false
! (Logical non) ! $n Returns True if the $n is not true, otherwise false

PS: It is important to note that logic and logic or both operators have four operational symbols ("&&", "and", "| |", "or"), although they belong to a logical structure, such as logic or (| | and OR), but they have different priorities. We will use it as an example to illustrate this later.


As for the priority, here is a simple comparison, 1+2*5 the result is 11 instead of 15, this is because the multiplication "*" priority is higher than the addition "+" priority level. Therefore, the PHP operator also has a priority.

Logical operator Instances

In this example we use the logic or the operation symbol in the "| |" and "or" do the same thing, but because "| |" and "or" have different priority, so they return the result is not the same, the code is as follows

<?phpheader ("Content-type:text/html;charset=utf-8");  What uses UTF-8 encoding $ A = true;             Declare a Boolean variable $ A and assign true $b = true;             Declares a Boolean variable $b, the assignment is true $c = false;            Declare a Boolean variable $CIF ($a or $b and $c) with a false initial value {      //to Judge    echo "true" with OR);} Else{    echo "false";} echo "<br/>", if ($a | | $b and $c) {      //in | | Make a judgment    echo "true";} Else{    echo "false";}? >

Code Run Result:

In the example above, we use the same if statement, except that we use different operators "or" and "| |", but the result of the return is exactly the opposite, so in practice, we must pay more attention to the precedence of operators

To the present we have introduced the "arithmetic operator", "string operator", "assignment Operator", "bitwise Operator", plus today's "logical operators", has learned five kinds of, the next section, we will explain the PHP operator of the sixth "comparison operator."

Related articles recommended:

1.PHP operator (i) An example of "arithmetic operator" explained

2.PHP operator (ii) a detailed example of a "string operator"

3.PHP operator (c) "Assignment operator" Example explained

4.PHP operator (iv) "Bitwise operator" Walkthrough

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.