Home >Backend Development >PHP Tutorial >A brief analysis of using PHP logical operators_PHP tutorial
PHP is still relatively commonly used, so I studied PHP logical operators and shared them with you here. I hope it will be useful to everyone. PHP is a powerful server-side scripting language for creating dynamic, interactive sites.
1. String operators
Example:
<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 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></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>b</FONT></SPAN><SPAN>=$a."World!"; </SPAN></SPAN><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></SPAN><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></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
The only array operator in PHP is the + operator. It appends the right array to the left array, but duplicate key values are not overwritten.
<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>
After execution, this script will display:
<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 logical operators :
PHP logical operators:
$aand$bAnd (logical AND) TRUE, if both $a and $b are TRUE.
$aor$bOr (logical OR) TRUE if either $a or $b is TRUE.
$axor$bXor (logical exclusive OR) TRUE, if either $a or $b is TRUE, but not both at the same time.
!$aNot (logical not) TRUE, if $a is not TRUE.
$a&&$bAnd (logical AND) TRUE, if both $a and $b are TRUE.
$a||$bOr (logical OR) TRUE if either $a or $b is TRUE.
The reason why "AND" and "OR" have two different forms of operators is that the priorities of their operations are different.