Home >Backend Development >PHP Tutorial >A brief analysis of using PHP logical operators_PHP tutorial

A brief analysis of using PHP logical operators_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:47871browse

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.

PHP is free and widely used. At the same time, PHP is undoubtedly another efficient option for competitors like Microsoft ASP. PHP is extremely suitable for website development, and its code can be directly embedded into HTML code. PHP syntax is very similar to Perl and C. PHP is often used with Apache (web server). However, it also supports ISAPI and can run on the Microsoft IIS platform for Windows. String Operators,There are two string operators. The first is the concatenation operator ("."), which returns the concatenated string of its left and right arguments. The second is the concatenation assignment operator (".="), which appends the right argument to the left argument.

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446483.htmlTechArticlePHP is still relatively commonly used, so I studied PHP logical operators and shared them with you here. , hope it is useful to everyone. PHP is a powerful way to create dynamic and interactive sites...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn