Home >Backend Development >PHP Problem >What is the difference between php logical operators

What is the difference between php logical operators

WBOY
WBOYOriginal
2022-03-25 14:52:322684browse

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:

<?php
$a = false || true; //&#39;||&#39;的优先级大于&#39;=&#39;,先执行 false || true,然后赋值$a
$b = false or true; // &#39;or&#39;的优先级小于&#39;=&#39;,先将false赋值给$b
var_dump($a).PHP_EOL;//true
var_dump($b);//false
?>

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

Equivalent to:

($a || $b) AND ($c || $d)

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!

Statement:
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