What do php logical operators mean?

青灯夜游
Release: 2023-03-15 10:38:01
original
4069 people have browsed it

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.

What do php logical operators mean?

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 operatorsExamplesWhen The result is true
&& or and (logical AND)$m && $n or $m and $nWhen $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 $nAs long as any one of $m and $n is true, the result is true
xor (logical exception Or) $m xor $nWhen $m and $n are one true and one false, the result is true
! (logical negation) !$mWhen $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:

= 18 && $age <= 25){
        echo '符合当兵条件!';
    }else{
        echo '不符合当兵条件!';
    }
?>
Copy after login

In the example we use The if else statement will be explained in detail later.

The running results are as follows:

符合当兵条件!
Copy after login

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

 25){
        echo '不符合当兵条件!';
    }else{
        echo '符合当兵条件!';
    }
?>
Copy after login

“!” 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:

 25)){
        echo '符合当兵条件!';
    }else{
        echo '不符合当兵条件!';
    }
?>
Copy after login

[Example] Determine whether the specified year is a leap year:

Copy after login

The running results are as follows:

2020年是闰年!
Copy after login

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!

Related labels:
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 [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!