算术运算符

还记得学校里学到的基本数学知识吗?就和它们一样。

算术运算符
例子名称结果
-$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的符号相同。例如:

<?phpecho (5 % 3)."\n";           // prints 2echo (5 % -3)."\n";          // prints 2echo (-5 % 3)."\n";          // prints -2echo (-5 % -3)."\n";         // prints -2?>  

参见手册中的数学函数。

User Contributed Notes

<a name="113646" "="">  
hassan dot beydoun at live dot com    10-Nov-2013 04:49    
<?php//using range function() //$numbers = array;range = value$numbers= range (1,100);// foreach ($array as $value) //{Do something with $value}foreach ($numbers as $value) {echo "<p>This is number: $value</P>";}?>      
antickon at gmail dot com    29-Mar-2012 08:04    
not listed here is the absolutely useless unary plus.<?php $a = -3;$a = +$a;var_dump( $a ); // int(-3)      
TheWanderer    05-Jun-2008 12:52    
It is worth noticing that when working with large numbers, most noticably using the modulo operator, the results depend on your CPU architecture. Therefore, running a decent 64-bit machine will be to your advantage in case you have to perform complex mathematical operations. Here is some example code - you can compare its output on x86 and x86_64 machines:<?php/* tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */$a = 2863311530;$b = 256;$c = $a % $b; echo "$c <br />\n"; echo (2863311530 % 256)." <br />\n"; /* directly with no variables, just to be sure */?> The code is expected to produce '170' if working correctly (try it in spreadsheet software).     
Jonathon Reinhart    16-Oct-2006 11:11    
A very simple yet maybe not obvious use of the modulus (%) operator is to check if an integer is odd or even.<?php   if (($a % 2) == 1)   { echo "$a is odd." ;}   if (($a % 2) == 0)   { echo "$a is even." ;}?> This is nice when you want to make alternating-color rows on a table, or divs.<?php   for ($i = 1; $i <= 10; $i++) {     if(($i % 2) == 1)  //odd       {echo "<div class=\"dark\">$i</div>";}     else   //even       {echo "<div class=\"light\">$i</div>";}    }?>      
glenn at benge dot co dot nz    06-Oct-2004 08:28    
a real simple method to reset an integer to a the next lowest multiple of a divisor $startSeq = $startSeq - ($startSeq % $entriesPerPage); if $startSeq was already a multiple, then " $startSeq % $entriesPerPage " will return 0 and $startSeq will not change.     
info at sima-pc dot com    01-May-2004 11:48    
Note that operator % (modulus) works just with integers (between -214748348 and 2147483647) while fmod() works with short and large numbers. Modulus with non integer numbers will give unpredictable results.     
<a name="37573" "="">  
soren at byu dot edu    20-Nov-2003 04:49    
Exponentiation doesn't use ^ or ** as you might be used to with other languages.  To calculate "z equals y to the x" use: $z = pow(y,x)     

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

触发修改文章时间【fastadmincms开发记录】|fastadmincms二次开发,触发,修改,文章,时间,fastadmin,ms,开发,记录
触发修改文章时间【fastadmincms开发记录】
在tp5中过滤输入的零宽度字符【fastadmincms开发记录】|fastadmincms二次开发,在tp5中,过滤,输入,的零,宽度,字符,fastadmin,ms,开发,记录
在tp5中过滤输入的零宽度字符【fastadmincms开发记录】
处理tag标签中的0宽空格【fastadmincms开发记录】|fastadmincms二次开发,处理,tag,标签,中的,0宽,空格,fastadmin,ms,开发,记录
处理tag标签中的0宽空格【fastadmincms开发记录】
添加专题时tags标签id出错【fastadmincms开发记录】|fastadmincms二次开发,添加,专题,时tags,标签,id,出错,fastadmin,ms,开发,记录
添加专题时tags标签id出错【fastadmincms开发记录】
20230518----模板 广告【fastadmincms开发记录】|fastadmincms二次开发,20230518,模板,广告,fastadmin,ms,开发,记录
20230518----模板 广告【fastadmincms开发记录】
cms添加视频模型【fastadmincms开发记录】|fastadmincms二次开发,ms,添加,视频,模型,fastadmin,开发,记录
cms添加视频模型【fastadmincms开发记录】
新增单篇收费复制功能【fastadmincms开发记录】|fastadmincms二次开发,新增,单篇,收费,复制,功能,fastadmin,ms,开发,记录
新增单篇收费复制功能【fastadmincms开发记录】
添加开会员折扣功能【fastadmincms开发记录】|fastadmincms二次开发,添加,开会,折扣,功能,fastadmin,ms,开发,记录
添加开会员折扣功能【fastadmincms开发记录】

历史上的今天:05月03日

热门专题

昆明网站建设|昆明网站建设,昆明网站开发,昆明网站建设公司,昆明网站建设价格,昆明网站设计,昆明网站制作,网页设计,高端网站建设,高端网站设计
昆明网站建设
云南开放大学|云南开放大学报名,云南开放大学报考,云南开放大学,什么是云南开放大学,云南开放大学学历,云南开放大学学费,云南开放大学报名条件,云南开放大学报名时间,云南开放大学学历,云南开放大学专业
云南开放大学
综合高中|云南综合高中,昆明综合高中,综合高中能考本一吗,综合高中和普通高中的区别,综合高中是什么意思,综合高中能参加全国统一高考吗,综合高中可以考哪些大学,综合高中的学籍是什么
综合高中
弥勒综合高中|弥勒综合高中
弥勒综合高中
卓越综合高中|卓越综合高中
卓越综合高中
APP开发|app开发_app开发公司_app软件开发_专业app开发_云南app开发公司_app定制_原生app开发定制
APP开发
金诺幼儿园(春城路金诺幼儿园)|昆明官渡区幼儿园,幼儿园报名,官渡区幼儿园,春城路幼儿园,幼儿园招生,学前班,昆明幼儿园,金诺幼儿园,环城南路幼儿园,石井路幼儿园
金诺幼儿园(春城路金诺幼儿园)
昆明综合高中|昆明综合高中
昆明综合高中

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部