If an expression participates in a logical operation, the first expression can determine the result of the entire logical expression, then the value of the second expression will not be calculated. This is a short-circuit operation. Let the editor lead you to learn together.
What is the short-circuit operation of &&
and ||
?
Short-circuit operation is also called short-circuit operator. Programming language designers believe that as long as there are logical operators (logical AND, logical OR), the result of the operation is true
or false
, and the running process is not important.
Logical OR||
When one of the two operations is true, the result is true. If both sides are false, the result is false.
<?php $a=true; $b=1; $a || ++$b; echo $b;//短路,上面的++$b被短路了,结果还是1 ?>
Logical AND&&
When one of the two operations is false, the result is false , if both sides are true, then the result is true. What are the benefits of the short-circuit operation of
<?php $a=false; $b=1; $a && ++$b; echo $b;//短路,上面的++$b被短路了,结果还是1 ?>
&&
and ||
?
If our operation is short-circuited, we can reduce the running time and improve the efficiency of the operation. At the same time, we can use the characteristics of the operation short-circuit to write a lot of concise code and reduce the time we write code. time.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Teach you how to perform short-circuit operations of PHP logical operators. For more information, please follow other related articles on the PHP Chinese website!