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!