首頁 >php教程 >php手册 >总结PHP运算符分类和使用

总结PHP运算符分类和使用

WBOY
WBOY原創
2016-06-13 11:05:00994瀏覽

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>
3):注意事项注意赋值运算将原变量的值拷贝到新变量中(传值赋值),所以改变其中一个并不影响另一个。这也适合于你在在紧密循环中拷贝一些值例如大数值。PHP 4 支持引用赋值,用 $var = &$othervar; 语法,但在 PHP 3 中不可能这样做。“引用赋值”意味着两个变量都指向同一个数据,没有任何数据的拷贝。PHP运算符应用技巧:赋值运算表达式的值也就是所赋的值。也就是说,“$a = 3”的值是 3。这样就可以使你做一些小技巧:
<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>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn