Home > Backend Development > PHP Tutorial > PHP Operator (5) 'Logical Operator' Example Explanation

PHP Operator (5) 'Logical Operator' Example Explanation

怪我咯
Release: 2023-03-07 12:26:02
Original
3868 people have browsed it

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 are logical operators?

Logical operations should be familiar to everyone. There is knowledge about logical operations in mathematics textbooks during school, which is what we often call "OR and NOT"; logical operators are in PHP programs. A very important set of operators used to combine the results of logical operations.

The logical operators in PHP are as follows


##Operator nameExampleResult##&& or and (logical AND)##||or or (logical OR) If at least one of $m and $n is true, return true, otherwise return false xor (logical exclusive OR) $m
$m and $n or $m && $n If both $m and $n are true, return true, otherwise return false

$m || $n or $m or $n

##! (logical not) ! $n If $n is not true, return true, otherwise return false

PHP Operator (5) Logical Operator Example ExplanationPS: It should be noted here that the two operators logical AND and logical OR have four operation symbols ("&&", "and", "||", "or"), although they They belong to the same logical structure, such as logical or (|| and or), but they have different priorities. We will use an example to illustrate this later.


Regarding priority, make a simple comparison here. The result of 1+2*5 is 11 instead of 15. This is because of the priority ratio of multiplication "*" The addition "+" has high priority. So PHP operators also have priority.

Logical Operator Example

In this example we use the operation symbols "||" and "or" in logical OR to perform the same judgment operation, but because "||" and "or" have different priorities, so the results they return are also different. The code is as follows

<?php
header("Content-type:text/html;charset=utf-8");  //什么使用UTF-8编码
$a = true;             //声明一个布尔型变量$a,赋值为真
$b = true;             //声明一个布尔型变量$b,赋值为真
$c = false;            //声明一个初值为假的布尔型变量$c

if($a or $b and $c){      //用or做判断
    echo "真";
}else{
    echo "假";
}

echo "<br/>";

if($a || $b and $c){      //用||做判断
    echo "真";
}else{
    echo "假";
}

?>
Copy after login

Code running results:

PHP Operator (5) Logical Operator Example Explanation

PHP Operator (5) Logical Operator Example ExplanationIn the above example, we used the same if statement, but used different operators "or" and "||", but the returned results are completely opposite, so in practical applications, it must Pay more attention to the details of operator priority.

So far I have introduced you to "arithmetic operators", "string operators", "assignment operators", "bit operators", plus Today, we have learned five types of "logical operators". In the next section, we will explain to you the sixth type of PHP operator "Comparison operator" .

Recommended related articles:

1.PHP Operator (1) "Arithmetic Operator" Example Example

2.PHP Operator (2) Detailed Example of "String Operator"

3. PHP Operator (3) "Assignment Operator" Example Example

4.PHP Operator (4) "Bit Operator" Example Example

The above is the detailed content of PHP Operator (5) 'Logical Operator' Example Explanation. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template