The similarities and differences between & and && in PHP logic operators

Source: Internet
Author: User
Tags arithmetic operators
The similarities and differences between & and && in PHP logic operators


PHP has 5 arithmetic operators (+-*/%), 6 assignment operators (+ = = *=/=%=. =), 8 comparison operators (= = = < > <= >=! = <>!==), and 6 logical operators (such as &&, | |, &, |,!. Where logic and & can be written and, logically or | written or, and some other operators ([email protected] execution operator anti-quote "-not single quotes, string operators.) ++/--、 ternary operator with. =, increment/decrement operator?:).

For operator Precedence, refer to the relevant documentation.


The similarities and differences between & and && in PHP

 4 && (+ + $a >10)) {}//output 11.echo $a;? >
 4 and (+ + $a >10)) {}//output is 11.echo $a;? >**************************************************************
 4 && (+ + $a <10)) {}//output 11.echo $a;? >
 4 & (+ + $a <10)) {}//output 11.echo $a;? >*********************************************************
 10) {}//output is 10.echo $a;? >
 10) {}//output is 11.echo $a;? >*******************************************************************
 
 *******************************************************************
 


Expression A $ A && $b, expression two $ A & $b

1, the same point: two expressions are when $ A, $b are true, the expression is true. The two operators have no effect on the result of this expression.

2, different points: the expression of $ A && $b && before the $ A is false, determined that the expression is false, the logical operator && after the $b is no longer evaluated, that is, the so-called operator "short circuit". And for the expression $ A & $b, regardless of the & before the true and false, the expression after the $b still to be calculated. Whether the $b after the logical operator is run may affect subsequent data and program results.

For logical operators | | and |, the similarities and differences ibid.



  • Related Article

    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.