Here is the experiment code: written in PHP, JavaScript should be the same:
I found that PHP and JavaScript are much worse! I append running results and code:
区别 | || or \& \& and
';echo '这里是$p = 6 or 0'.'结果,是【';$p = 6 or 0;var_dump($p);//int(6)echo '】
';echo '这里是$p = 6 | 0'.'结果,是【';$p = 6 | 0;var_dump($p);echo '】
';echo '这里是$p = 6 || 0'.'结果,是【';$p = 6 || 0;var_dump($p);//bool(true)echo '】
';echo '
';echo '这里是$p = 6 and 0'.'结果,是【';$p = 6 and 0;var_dump($p);echo '】
';echo '这里是$p = 6 & 0'.'结果,是【';$p = 6 & 0;var_dump($p);echo '】
';echo '这里是$p = 6 && 0'.'结果,是【';$p = 6 && 0;var_dump($p);echo '】
';
This is just beginning to learn to be confused, I searched the information, also do not understand, the conclusion is = priority is greater than | , &,
I still don't understand ...
In fact | and & Good understanding, is | | And && do not understand, but also look Daniel answer.
Reply content:
Here is the experiment code: written in PHP, JavaScript should be the same:
I found that PHP and JavaScript are much worse! I append running results and code:
区别 | || or \& \& and
';echo '这里是$p = 6 or 0'.'结果,是【';$p = 6 or 0;var_dump($p);//int(6)echo '】
';echo '这里是$p = 6 | 0'.'结果,是【';$p = 6 | 0;var_dump($p);echo '】
';echo '这里是$p = 6 || 0'.'结果,是【';$p = 6 || 0;var_dump($p);//bool(true)echo '】
';echo '
';echo '这里是$p = 6 and 0'.'结果,是【';$p = 6 and 0;var_dump($p);echo '】
';echo '这里是$p = 6 & 0'.'结果,是【';$p = 6 & 0;var_dump($p);echo '】
';echo '这里是$p = 6 && 0'.'结果,是【';$p = 6 && 0;var_dump($p);echo '】
';
This is just beginning to learn to be confused, I searched the information, also do not understand, the conclusion is = priority is greater than | , &,
I still don't understand ...
In fact | and & Good understanding, is | | And && do not understand, but also look Daniel answer.
Title to modify the topic, added JS content, I also added JS related answers:
JS bit operations ( | , & ) and PHP similar, not detailed. The main explanation is the difference of the logical operation.
Many programming languages (including JS, but not PHP) are optimized for logical operations based on performance considerations
For and operations a = b && c , the value of a is: if B is logically true, return C; otherwise return B
For an OR Operation a = b || c , the value of a is: if B is logically true, returns B;
Give a few examples
a = 1 && 2; // 1a = null && 2; // nulla = 'str' || 2; // 'str'a = undefined || 2; // 2
In fact, using the following example can be more intuitive expression
function c() { console.log('这里不会调用');}a = null && c(); // a 为 null
Because the && front is false, NULL is returned directly, and subsequent functions c() are not called.
(I will not elaborate on the principle, I know that performance optimization is enough.) If you are a computer professional, you must have said it in the theory class, but you may not have listened carefully. )
Here are PHP the original answers for
Just have time, I will elaborate.
The first and or && || is the logical operators, the functions are identical, the only difference is the priority: && || > Assignment operator ( = ) > and or ;
There's a less obvious hole in your test code.
Because the priority: || > = > or , so
$p = 6 or 0;
The actual order of operations is
($p = 6) or 0; // $p 为 6
If you change into
$p = (6 or 0); // $p 为 true
is consistent with || the use of.
2. & | is a bitwise operator, and the above is not an operation.
A bitwise operator is the result of a logical operation of each bit of a number, such as
$b = (10 | 12); // $b 为 14
10 (binary) 1010 and 12 (binary 1100 ) bit and operation, get 14 (binary 1110 )
In JS | | ,&& used a lot.
The logical operation of the bitwise operation is different, hoping to read more books. | and & is a bitwise operation, | | And && is a logical operation
Most languages have && | | Symbol bar, the meaning of a few are the same, are as logical characters, you ask, I feel you have not learned programming ...