php有很多值得学习的地方,这里我们主要介绍php逻辑运算符,包括介绍位运算符、比较运算符等方面。位运算符允许对整型数中指定的位进行置位。如果左右参数都是字符串,则位运算符将操作这个字符串中的字符。
1.PHP逻辑运算符之位运算符:
<OL class=dp-xml><LI class=alt><SPAN><SPAN>&:$a & $b And(按位与) 将在 $a 和 $b 中都为 1 的位设为 1。 </SPAN></SPAN><LI class=""><SPAN>|:$a | $b Or(按位或) 将在 $a 或者 $b 中为 1 的位设为 1。 </SPAN><LI class=alt><SPAN>^:$a ^ $b Xor(按位异或) 将在 $a 和 $b 中不同的位设为 1。 </SPAN><LI class=""><SPAN>~:~ $a Not(按位非) 将 $a 中为 0 的位设为 1,反之亦然。 </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag><</SPAN></FONT></STRONG><SPAN>:$a </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag><</SPAN></FONT></STRONG><SPAN> $b Shift left(左移) 将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”)。 </SPAN></SPAN><LI class=""><SPAN>$a </SPAN><STRONG><FONT color=#006699><SPAN class=tag>></SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> $b Shift right(右移) 将 $a 中的位向右移动 $b 次(每一次移动都表示“除以 2”)。 </SPAN></SPAN></LI></OL>
位运算符号涉及到微观的位变化,举个例子 5&3 那么5的存储二进制代码是 0110 3的存储二进制代码是0100 那么5与3做逻辑与预算即为
<OL class=dp-xml><LI class=alt><SPAN><SPAN>0101 </SPAN></SPAN><LI class=""><SPAN>0011 </SPAN><LI class=alt><SPAN>0001 (结果为1) </SPAN></LI></OL>
那么结果还是1,例子如下:
<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>php常量定义--阿涛随笔</SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>a</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>5</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>b</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>3</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>c</FONT></SPAN><SPAN>=$a&$b; </SPAN></SPAN><LI class=""><SPAN>echo $c."</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>"; </SPAN></SPAN><LI class=alt><SPAN>echo 5&3; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN></LI></OL>
为运算需要二进制方面的知识,一般用不到位运算。
立即学习“PHP免费学习笔记(深入)”;
2.PHP逻辑运算符之比较运算符:
比较运算符,如同它们名称所暗示的,允许你对两个值进行比较
<OL class=dp-xml><LI class=alt><SPAN><SPAN>==:等于 返回值 true or </SPAN><SPAN class=attribute>falsh</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>===:全等 返回值 true or falsh (PHP 4 only) </SPAN><LI class=alt><SPAN>!=:不等 返回值 true or falsh </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=tag>></SPAN><SPAN>:不等 返回值 true or falsh 。 </SPAN></SPAN><LI class=alt><SPAN>!==:非全等 返回值 true or falsh 。PHP 4 only)。 </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN>:小与 非全等 返回值 true or falsh。 </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag>></SPAN><SPAN>:大于 非全等 返回值 true or falsh。 </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN>=:非全等 返回值 true or falsh 。 </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag>></SPAN><SPAN>= :非全等 返回值 true or falsh 。 </SPAN></SPAN></LI></OL>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号