Below is the experiment code: written in php, JavaScript should be similar: I found that php and JavaScript are too bad! I appended the running result and code: {code ...} I was confused at the beginning. I searched for the information and did not understand it. The reliable conclusion is that the priority is higher than |, & amp ;...
The following is the experiment code: written in php, and JavaScript should be similar:
I found that php and JavaScript are much worse! I appended the running result and code:
Difference | or \ & andScript document. write ('Below is JavaScript:
'); Document. write ('var p = 6 | 0 here. The result is ['); var p = 6 | 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('var p = 6 | 0 here. The result is ['); var p = 6 | 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('
'); Document. write ('var p = 6 & 0 here. The result is ['); var p = 6 & 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('here is var p = 6 & 0, and the result is ['); var p = 6 & 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Script
'; Echo' Here is $ p = 6 or 0 '. 'result, Which is ['; $ p = 6 or 0; var_dump ($ p); // int (6) echo ']
'; Echo' here is the result of $ p = 6 | 0'. ', which is ['; $ p = 6 | 0; var_dump ($ p); echo ']
'; Echo' Here is $ p = 6 | 0 '. 'result, Which is ['; $ p = 6 | 0; var_dump ($ p); // bool (true) echo ']
'; Echo'
'; Echo' here is the result of $ p = 6 and 0'. ', which is ['; $ p = 6 and 0; var_dump ($ p); echo ']
'; Echo' here is the result of $ p = 6 & 0'. ', which is ['; $ p = 6 & 0; var_dump ($ p); echo ']
'; Echo' here is the result of $ p = 6 & 0'. ', which is ['; $ p = 6 & 0; var_dump ($ p); echo ']
';
I was confused at the beginning. I searched for information and did not understand it. The reliable conclusion is that = Priority is higher than | ,&,
I still don't understand...
Actually | and & easy to understand, that is | and & don't understand, hope you can answer this question ..
Reply content:
The following is the experiment code: written in php, and JavaScript should be similar:
I found that php and JavaScript are much worse! I appended the running result and code:
Difference | or \ & andScript document. write ('Below is JavaScript:
'); Document. write ('var p = 6 | 0 here. The result is ['); var p = 6 | 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('var p = 6 | 0 here. The result is ['); var p = 6 | 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('
'); Document. write ('var p = 6 & 0 here. The result is ['); var p = 6 & 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Document. write ('here is var p = 6 & 0, and the result is ['); var p = 6 & 0; document. write (typeof (p); document. write ('+ p); document. write (')]
'); Script
'; Echo' Here is $ p = 6 or 0 '. 'result, Which is ['; $ p = 6 or 0; var_dump ($ p); // int (6) echo ']
'; Echo' here is the result of $ p = 6 | 0'. ', which is ['; $ p = 6 | 0; var_dump ($ p); echo ']
'; Echo' Here is $ p = 6 | 0 '. 'result, Which is ['; $ p = 6 | 0; var_dump ($ p); // bool (true) echo ']
'; Echo'
'; Echo' here is the result of $ p = 6 and 0'. ', which is ['; $ p = 6 and 0; var_dump ($ p); echo ']
'; Echo' here is the result of $ p = 6 & 0'. ', which is ['; $ p = 6 & 0; var_dump ($ p); echo ']
'; Echo' here is the result of $ p = 6 & 0'. ', which is ['; $ p = 6 & 0; var_dump ($ p); echo ']
';
I was confused at the beginning. I searched for information and did not understand it. The reliable conclusion is that = Priority is higher than | ,&,
I still don't understand...
Actually | and & easy to understand, that is | and & don't understand, hope you can answer this question ..
The subject modified the question and added the js content. I also added the js-related answer:
Js bitwise operations (|
,&
) Similar to php, not detailed. This section describes the differences in logical operations.
Many programming languages (including js, but not php) have optimized logical operations based on performance considerations.
And operationsa = b && c
, The value of a is:If B is logically true, c is returned; otherwise, B is returned.
Or operationsa = b || c
, The value of a is:If B is logically true, B is returned; otherwise, c is returned.
Examples
a = 1 && 2; // 1a = null && 2; // nulla = 'str' || 2; // 'str'a = undefined || 2; // 2
In fact, the following example can be used for more intuitive display.
Function c () {console. log ('won't be called here');} a = null & c (); // a is null
Because&&
The previous function is false, so null is returned directly.c()
Will not be called.
(I will not elaborate on the principle. I know that performance optimization is enough. If you are a computer professional, you must have talked about it during the theoretical course, but you may not have listened carefully at the time .)
ForPHP
Original answer
I will elaborate on the time.
Firstand
or
&&
||
All are logical operators with identical functions. The only difference is priority:&&
||
> Value assignment operator (=
)>and
or
;
There is an obvious pitfall in your test code.
Because priority:||
>=
>or
, So
$p = 6 or 0;
The actual operation order is
($ P = 6) or 0; // $ p is 6
If it is changed
$ P = (6 or 0); // $ p is true
Just use||
Yes.
2. While & | is a bitwise operator, which is not an operation above.
Bitwise operators are the results of logical operations on each digit of a number, such
$ B = (10 | 12); // $ B is 14
10 (binary is1010
) And 12 (Binary1100
) Bitwise and operation, get 14 (Binary1110
)
In JS |, & A Lot
Bit operations have different logic operations. I hope to read more books. | Bitwise operation, | and
Most languages have the & | symbol, which has the same meaning and serves as a logical operator. When you ask this question, I feel that you have never learned programming...