Home  >  Article  >  Backend Development  >  Teach you how to perform short-circuit operations of PHP logical operators

Teach you how to perform short-circuit operations of PHP logical operators

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-03-24 11:34:001963browse

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.

Teach you how to perform short-circuit operations of PHP logical operators

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!

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