Rumah >pembangunan bahagian belakang >tutorial php >浅析使用PHP逻辑运算符_PHP教程
PHP还是比较常用的,于是我研究了一下PHP逻辑运算符,在这里拿出来和大家分享一下,希望对大家有用。PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。
1.字符串运算符
例子:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Hello"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=$a."World!"; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Hello"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">a.</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"World!"</font></span><span>; </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
PHP仅有的一个数组运算符是+运算符。它把右边的数组附加到左边的数组后,但是重复的键值不会被覆盖。
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("a"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"apple","b"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"banana"); </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("a"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"pear","b"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"strawberry","c"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"cherry"); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span>=$a+$b; </span> </li> <li class=""><span>$ar_dump($c); </span></li> </ol>
执行后,此脚本会显示:
<ol class="dp-xml"> <li class="alt"><span><span>array(3){ </span></span></li> <li class=""> <span>["a"]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"><span>string(5)"apple" </span></li> <li class=""> <span>["b"]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"><span>string(6)"banana" </span></li> <li class=""> <span>["c"]=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"><span>string(6)"cherry" </span></li> <li class=""><span>} </span></li> </ol>
2.PHP逻辑运算符:
PHP逻辑运算符:
$aand$bAnd(逻辑与)TRUE,如果$a与$b都为TRUE。
$aor$bOr(逻辑或)TRUE,如果$a或$b任一为TRUE。
$axor$bXor(逻辑异或)TRUE,如果$a或$b任一为TRUE,但不同时是。
!$aNot(逻辑非)TRUE,如果$a不为TRUE。
$a&&$bAnd(逻辑与)TRUE,如果$a与$b都为TRUE。
$a||$bOr(逻辑或)TRUE,如果$a或$b任一为TRUE。
“与”和“或”有两种不同形式运算符的原因是它们操作的优先级不同。