©
このドキュメントでは、 php中国語ネットマニュアル リリース
还记得学校里学到的基本数学知识吗?就和它们一样。
例子 | 名称 | 结果 |
---|---|---|
-$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 的符号相同。例如:
<?php
echo ( 5 % 3 ). "\n" ; // prints 2
echo ( 5 % - 3 ). "\n" ; // prints 2
echo (- 5 % 3 ). "\n" ; // prints -2
echo (- 5 % - 3 ). "\n" ; // prints -2
?>
参见手册中的数学函数。
[#1] Hayley Watson [2015-03-12 22:36:25]
Not mentioned is the unary + operator, which is equivalent to adding its operand to zero. When applied to a number it has no effect, but to other types it has the effect of converting the value to an integer or double, whichever is more appropriate for the value and according to the usual type juggling rules.
<?php
echo gettype(+'76777.5'); // outputs "double"
echo gettype(+'767775'); // outputs "integer"
?>
[#2] Dominik Buechler [2014-12-05 13:01:07]
In addition to Jonathan's comment, there is a way simpler way to determine if an integer is even or not:
<?php $odd = $i % 2; ?>
or
<?php $even = !($i % 2); ?>
This works because a modulo division by 2 will always return either 0 or the rest 1. Since those are valid boolean values you can just invert them by adding a prefixed ! if wanted.
[#3] lmc at trendicy dot com [2014-09-09 05:50:10]
If you are running a php version older than 5.6, you can calculate $a ** $b by using exp($b*log($a))
[#4] hassan dot beydoun at live dot com [2013-11-09 19:49:28]
<?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>";}
?>
[#5] Andrew [2013-07-17 14:10:52]
The % operator doesn't behave as many people with a maths background would expect, when dealing with negative numbers. For example, -1 mod 8 = 7, but in PHP, -1 % 8 = -1.
The following function has the expected behaviour:
function mod($a, $n) {
return ($a % $n) + ($a < 0 ? $n : 0);
}
mod(-1, 8) returns 7 as expected.
[#6] antickon at gmail dot com [2012-03-28 23:04:04]
not listed here is the absolutely useless unary plus.
<?php
$a = -3;
$a = +$a;
var_dump( $a ); // int(-3)
[#7] php at richardneill dot org [2011-04-18 08:13:23]
For larger numbers (above PHP_INT_MAX), use fmod() rather than %.
The other operators (+-*/) work correctly with floats and integer overflow, but % uses integer wrap. Eg.
<?php
var_dump(0xffffffff % 2);
//Prints int(-1) which is WRONG
var_dump(intval(fmod(0xffffffff,2)));
//Prints int(1) which is the right answer
?>
(The reason this matters is that PHP's float is actually a double, and can accurately represent integers up to 52-bits, even on 32-bit systems)
[#8] TheWanderer [2008-06-05 03:52:03]
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
$a = 2863311530;
$b = 256;
$c = $a % $b;
echo "$c <br />\n";
echo (2863311530 % 256)." <br />\n";
?>
The code is expected to produce '170' if working correctly (try it in spreadsheet software).
[#9] calmarius at atw dot hu [2008-05-15 06:19:17]
Be careful when using % with large numbers.
The code:
<?php
echo 3333333333 % 3
?>
puts out -1 instead of zero!
(Due to the overflow)
[#10] Jonathon Reinhart [2006-10-16 02:11:54]
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>";}
}
?>
[#11] pww8 at cornell dot edu [2005-08-17 14:25:28]
It appears floating-point infinity (INF) is not returned from divide by zero (in PHP 5.0.0). Instead a warning is given and Boolean FALSE is returned.
I searched the various manuals and did not find relevant explanation, so am adding this.
[#12] glenn at benge dot co dot nz [2004-10-05 23:28:57]
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.
[#13] arjini at gmail dot com [2004-09-08 09:48:36]
When dealing purely with HTML, especially tables, or other things in "grids" the modulous operator is really useful for splitting up the data with a seperator.
This snippet reads any gif files from the directory the script is in, prints them out and puts in a break every 5th image.
<?php
$d = dir('./');
$i = 0;
while(false !== ($e = $d->read())){
if(strpos($e,'.gif')){
++$i;
echo '<img src="'.$e.'"/>'.chr(10);
if(!($i%5))
echo '<br/>';
}
}
?>
For tables just put </tr><tr> in place of the break.
[#14] info at sima-pc dot com [2004-05-01 14:48:33]
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.