Home  >  Article  >  Backend Development  >  Introduction to the use of logical operators in php_PHP tutorial

Introduction to the use of logical operators in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:211118browse

Logical operators in php include not equal to, or, and, and not (the priority is: ! > && > || > and > xor > or). Let me introduce to you the logical operations in php. How to use the symbol.

Note:

The priority is: ! > && > || > and > xor > or
Also note that although ! has higher precedence than =, PHP still allows expressions like the following: if (!$a = foo()), in this case the output of foo() is assigned to $a .

First look at the manual and I agree with a sentence in the manual: using brackets can enhance the readability of the code.

例子 名称 结果
$a and $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
$a or $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE
$a xor $b Xor(逻辑异或) TRUE,如果 $a 或 $b 任一为 TRUE,但不同时是。
! $a Not(逻辑非) TRUE,如果 $a 不为 TRUE
$a && $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
$a || $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE

Example

In php, "|" is a php bit operator, and "||" is a logical operator

Bitwise operator code:

The code is as follows
 代码如下 复制代码
$a=0;
$b=0;
if($a=3 | $b=3){
$a++;
$b++;
}
echo $a.','.$b; //输出 4,4
?>
Copy code

代码如下 复制代码
$a=0;
$b=0;
if($a=3 || $b=3){
$a++;
$b++;
}
echo $a.','.$b; //输出 1,1
?>
$a=0;

$b=0;

if($a=3 | $b=3){

$a++;
$b++;

} echo $a.','.$b; //Output 4,4 ?> Compare the code, the following is the code of the logical operator:
The code is as follows Copy code
$a=0;<🎜> $b=0;<🎜> if($a=3 || $b=3){<🎜> $a++;<🎜> $b++;<🎜> }<🎜> echo $a.','.$b; //Output 1,1<🎜> ?> In the above two examples, in the first example, "$a=3 | $b=3", since the priority of "|" is higher than the assignment operator, the order of operations can be written as "$a =(3 | $b=3)", first $b is assigned the value 3, and $a is assigned the value of the binary number 0100 | 0100, which is still 0100, so $a is assigned the value 0100 at this time, which is decimal 3. The assignment is successful, true is returned, and the content in the if code block is executed. $a is incremented, and $b is also incremented. Therefore, $a=4, $b=4 In the second example, it can also be seen as "$a = (3 || $b = 3)". First, 3||$b=3 returns true, "||" causes a short circuit, and the "||" before 3 is already true, "$b=3" is no longer executed, so $b is still 0 at this time, $a is true of Boolean type, the assignment is successful, true is returned, the content in the if code block is executed, and $a++ returns is true, $b++ is 1, so, $a=1,$b=1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628701.htmlTechArticleLogical operators in php include not equal to, or, and, and not (the priority is:! >> || > and > xor > or), let me introduce to you how to use logical operators in php. Note:...
Statement:
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