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.