PHP运算符是我们在程序中最常用的,最近看了一本PHP的书深入的介绍了PHP运算符的使用,这里就和大家一起分享一下吧。
1:算数运算符:
-:-$a 取反 $a 的负值。
+:$a + $b 加法 $a 和 $b 的和。
-:$a - $b 减法 $a 和 $b 的差。
*:$a * $b 乘法 $a 和 $b 的积。
/:$a / $b 除法 $a 除以 $b 的商。
%:$a % $b 取模 $a 除以 $b 的余数。
注意:除号(“/”)总是返回浮点数,即使两个运算数是整数(或由字符串转换成的整数)也是这样取模 $a % $b 在 $a 为负值时的结果也是负值。
例子:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></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> </li> <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> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><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">3</font></span><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">4</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">5</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">d</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">6</font></span><span>; </span> </li> <li class="alt"><span> </span></li> <li class=""> <span>//$</span><span class="attribute"><font color="#ff0000">d</font></span><span>=-$a; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">e</font></span><span>=$a+$b; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">f</font></span><span>=$d-$c; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">g</font></span><span>=$a*$d; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">h</font></span><span>=$d/$a; </span> </li> <li class="alt"> <span>echo "{$e}</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class=""> <span>echo "{$f}</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class="alt"> <span>echo "{$g}</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class=""> <span>echo "{$h}</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
2:赋值运算符:
基本的赋值运算符是“=”。你一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左运算数。
1):简单赋值:
<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">"http://www.admin300.com"</font></span><span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">2008</font></span><span> </span></span></li></ol>
<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> = ($</span><span class="attribute"><font color="#ff0000">b</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">4</font></span><span>) + 5; </span></span></li></ol>