What is the difference between php logical operators

WBOY
Release: 2023-03-15 16:44:01
Original
2523 people have browsed it

Difference: 1. The and and "&&" operators are both used to express logical AND, and has a lower priority and "&&" has a higher priority; 2. or and "||" operators Both are used to express logical OR, or has a lower priority and "||" has a higher priority; 3. The "$" operator is used to express logical NOT; 4. The xor operator is used to express logical exclusive OR.

What is the difference between php logical operators

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the difference between php logical operators

php logical operators

What is the difference between php logical operators

I have always thought that "or" and "|" in PHP "|" can be equalized, but it is not the case. There is a priority difference between them. The following is an example to illustrate their differences:

Copy after login

After execution, $a is true and $b is false. This is because the priority of "||" is greater than "=". False || true is executed first, and then the result true after execution is assigned to $a. The priority of "or" is less than "=", first assign false to $b, so $b is false

The logical "AND" operation in PHP has two forms: AND and &&, the same The "or" operation also has two forms: OR and ||.

If two separate expressions participate in the operation, the results of the two forms are exactly the same. For example, there is no difference between $a AND $b and $a && $b. The same $a OR $b and $a || $b are exactly the same.

But the two forms of logical operators have different priorities. The priorities of these four symbols from high to low are: &&, ||, AND, OR. Pay special attention to this sentence and understand it carefully. Example:

$a || $b AND $c || $d
Copy after login

Equivalent to:

($a || $b) AND ($c || $d)
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What is the difference between php logical operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!