PHP logical Operators & and && and && and | | The Use Difference

Source: Internet
Author: User
A logical operator is nothing more than a logical operation of a value. (&&) or (| | These two operators can speed up the PHP code in the program.

First look at a piece of code:

<?php       $test = "John Doe";       $test = = "Zhang San" && $test = "Zhang San";       echo $test;  Output "John Doe"       $test = "John Doe";       $test = = "Zhang San" | | $test = "Zhang Three is not here";       echo $test;  Output "Zhang Three is not here"   ?>

Why does it produce such a result? If we follow the usual method, we should at least use an if statement to judge. But now just two logical operations will change the value of the variable. Let's examine how it works.

In both sides of the expression that participates in the logical operation, it is performed in the left-to-right order. As long as one of the "and" operations is false, the result of the entire expression is false. So, when the left-hand expression is false, no more operations are needed. Such treatment is undoubtedly of great benefit to the operational efficiency of the program. So, as the topic says, it's an efficient usage. And logic is different: As long as one is true, the whole expression is true. So, in the case of false on the left, also run the expression on the right to judge. To understand or understand what is said above, it is not strange to the result.

Of course, the above example can be achieved by conditional judgment Statement, now the situation is to reduce the amount of code, the most important thing is to increase the execution efficiency of the program. The key to mastering this is the direction in which the expression runs, from left to right. When the first value determines the value of the entire expression, it stops running. It is worth noting that the right side can be an expression or a function, but not a series of statement combinations or output statements. After all, it is also an integral part of the logical expression.

For "and" (&&) operations: x && y when x is false, skip directly, do not execute y;
For "or" (| | ) Operation: x| | Y when X is true, skip directly and do not execute y.

The similarities and differences between & and && in PHP

<?php$a=10;if ($a >4 && (+ $a >10)) {}//output is 11.echo $a;? ><?php$a=10;if ($a >4 and (+ + $a >10)) {}//output is 11.echo $a;? >**************************************************************<?php$a=10;if ($a >4 && (+ + $a < 10) {}//output is 11.echo $a;? ><?php$a=10;if ($a >4 & (+ $a <10)) {}//output is 11.echo $a;? >*********************************************************<?php$a=10;if ($a <4 && (+ $a >10)) {} The output is 10.echo $a;? ><?php$a=10;if ($a <4 & (+ $a >10)) {}//output is 11.echo $a;? >*******************************************************************<?php$a=10;if ($a <4 && (+ $a &LT;10) {}//output is 10.echo $a;? ><?php$a=10;if ($a <4 & (+ $a <10)) {}//output is 11.echo $a;? >*******************************************************************<?php//the following sktest () are not called because they are "shorted" by the operator. $a = (false && sktest ()), $b = (true | | sktest ()), $c = (false and Sktest ()), $d = (true or sktest ());//"| |" Priority higher than "or" $e = False | | True The $e is assigned a value of (false | | true) and the result is True$f = False or true; $f is assigned false [Altair NOTE: "=" has a higher priority than "or"]var_dump ($e, $f);//"&&" has a higher priority than "and" $g = True && false; The $G is assigned a value of (true && false), and the result is False$h = True and false; $h is assigned true [Altair NOTE: "=" has a higher priority than "and"]var_dump ($g, $h);? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.