PHP operator (5) "logical operator" example

Source: Internet
Author: User
Tags php operator
We should be familiar with logical operations. during school hours, there was a knowledge of logical operations in the mathematics textbooks, which we often say "or" not "; logical operators are a group of very important operators in PHP programs. they are used to combine the results of logical operations. Today I will explain to 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?

We should be familiar with logical operations. during school hours, there was a knowledge of logical operations in the mathematics textbooks, which we often say "or" not "; logical operators are a group of very important operators in PHP programs. they are used to combine the results of logical operations.

The logical operators in PHP are as follows:


Operator name Example Result
& OR and (logical and) $ M and $ n or $ m & $ n If both $ m and $ n are true, true is returned. otherwise, false is returned.

| Or (logical or)

$ M | $ n or $ m or $ n If either $ m or $ n is true, true is returned. otherwise, false is returned.
Xor (logical exclusive or) $ M xor $ n If $ m and $ n have one and only one of them is true, true is returned. otherwise, false is returned.
! (Non-logical) ! $ N If $ n is not true, true is returned; otherwise, false is returned.

For priority, here is a simple comparison. the result of 1 + 2*5 is 11 rather than 15, because the priority of multiplication "*" is higher than that of addition method "+. Therefore, PHP operators also have priority.

Logical operators

In this instance, we use the operators "|" and "or" in the logic or to perform the same judgment operation, but because "|" and "or" have different priority, the returned results are also different. the code is as follows:

 "; If ($ a | $ B and $ c) {// use | to judge echo" true ";} else {echo" false ";}?>

Code running result:

In the above example, we use the same if statement, except that different operators "or" and "|" are used, but the returned results are the opposite, therefore, in practice, pay more attention to the operator priority details,

Up to now, I have introduced "arithmetic operators", "string operators", "value assignment operators", "bit operators", and today's "logical operators, in the next section, we will explain the sixth "comparison operator" of the PHP operator ".

Recommended articles:

1. PHP operator (1) "arithmetic operator" example

2. PHP operator (2) "string operator"

3. PHP operator (3) "value assignment operator" example

4. PHP operator (4) "bit operator" example

The above is the details of the PHP operator (5) "logical operator" example. For more information, see other related articles in the first PHP community!

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.