Home > Article > Backend Development > What do php logical operators mean?
In PHP, logical operator is a symbol for logical operations. It can be used to combine the results of logical operations. It is a very important group of operators in programming; there are 6 types of logical operators: " and" and "&&" represent logical AND, "||" and "or" represent logical OR, "xor" represents logical exclusive OR, and "!" represents logical negation.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, logical operators are used to perform logic A symbol for operations.
Logical operators are used to combine the results of logical operations and are a very important set of operators in programming.
Logical operators in PHP are shown in the following table:
Logical operators | Examples | When The result is true |
---|---|---|
&& or and (logical AND) | $m && $n or $m and $n | When $m and $ The result is true when n are both true, and the result is false when either $m and $n are false |
|| or or (logical OR) | $ m || $n or $m or $n | As long as any one of $m and $n is true, the result is true |
xor (logical exception Or) | $m xor $n | When $m and $n are one true and one false, the result is true |
! (logical negation) | !$m | When $m is false, the result is true |
"&&" or "and" Logical AND
When the left and right conditions are TRUE at the same time, the result is TRUE; when any one of the two conditions is FALSE, the result is FALSE. When the condition on the left is FALSE, the condition on the right will be skipped and FALSE will be returned directly.
[Example] Assume that people between the ages of 18 and 25 meet the conditions for military conscription, and Xiao Ming is 21 years old this year. To determine whether Xiao Ming is suitable for military service, the implementation code is as follows:
<?php $age = 21; if($age >= 18 && $age <= 25){ echo '符合当兵条件!'; }else{ echo '不符合当兵条件!'; } ?>
In the example we use The if else statement will be explained in detail later.
The running results are as follows:
符合当兵条件!
"||" or "or" logical OR
If one of the two conditions is TRUE, the result Is TRUE; if both conditions are FALSE, the result is FALSE. When the left condition is TRUE, the judgment of the right condition will be skipped and TRUE will be returned directly.
[Example] Adjust the above example and use the logical OR operator. The code is as follows:
<?php $age = 21; if($age < 18 || $age > 25){ echo '不符合当兵条件!'; }else{ echo '符合当兵条件!'; } ?>
“!” Logical NOT
Negate a Boolean value. For example: !true = false, !false = true, !10 = false.
[Example] Use the same previous example and implement it using the logical NOT operator. The code is as follows:
<?php $age = 21; if(!($age < 18 || $age > 25)){ echo '符合当兵条件!'; }else{ echo '不符合当兵条件!'; } ?>
[Example] Determine whether the specified year is a leap year:
<?php $year = 2020; if($year%4==0 && $year%100!=0 || $year%400==0){ echo $year.'年是闰年!'; }else{ echo $year.'不年是闰年!'; } ?>
The running results are as follows:
2020年是闰年!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What do php logical operators mean?. For more information, please follow other related articles on the PHP Chinese website!