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
Example | Result | |
$m and $n or $m && $n | If both $m and $n are true, return true, otherwise return false | |
$m || $n or $m or $n | If at least one of $m and $n is true, return true, otherwise return falsexor (logical exclusive OR) | |
##! (logical not) | ! $n | If $n is not true, return true, otherwise return false |
PS: 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: In 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" |
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!