Php or php

Source: Internet
Author: User

Php or php

1. Today I met a php or arithmetic interview question. It's very interesting. I 'd like to share it with you. I don't know why.

<? Php $ a = 0; $ B = 0; if ($ a = 3)> 0 | ($ B = 3)> 0) {$ a ++; $ B ++;} echo $ a; // 4 echo $ B; // 1if ($ a = 3)> 0 | ($ B = 3)> 0) {$ a ++; $ B ++;} echo $ a; // 4 echo $ B; // 4

Explanation:

First if judgment: This is a Boolean short-circuit operation. | when the first expression of the operation is satisfied, expression 2 is not calculated. Therefore, the value assignment of $ B = 3 is not executed. Such as if (x | y). if the first condition x is true, the subsequent judgment will not be executed, therefore, in the above Code, if $ B = 3 is not executed, B is still 0, so after ++, it is still 1.

|: Checks the authenticity of each condition and performs "or" operations (the bachelor is not afraid of death)
|: According to the conditional write order, until one is true, the following conditions are not checked, directly entering the condition
&: Checks the authenticity of each condition and performs the "and" operation (the bachelor is not afraid of death)
&: In the conditional write order, when one is false, the following conditions will not be checked and will jump out directly.
& Is a bitwise operator that represents bitwise AND operation. & Is a logical operator that can be short-circuited.

 

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.