PHP 5.5.16 is released


学校で習った基礎代数を憶えていますか? この演算子はそれらと同様に動作します。

名前 結果
-$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$b 乗。PHP 5.6 で導入されました。

除算演算子 ("/") の返す値は浮動小数点数となります。 ただし、ふたつのオペランドがともに整数 (あるいは整数に変換できる文字列) であり、かつ結果が割り切れる場合には整数値を返します。

剰余演算子は、まず両方のオペランドを整数に直し (小数点以下を切り捨てます) てから処理を行います。

剰余演算子 % の結果の符号は、被除数の符号と同じになります。 つまり、$a % $b の結果の符号は $a と同じになるということです。


echo (3)."\n";           // 2
echo (% -3)."\n";          // 2
echo (-3)."\n";          // -2
echo (-% -3)."\n";         // -2


マニュアルの 数学関数の項も参照してください。

add a note add a note

User Contributed Notes 11 notes

arjini at gmail dot com
9 years ago
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.

= dir('./');
$i = 0;
false !== ($e = $d->read())){
'<img src="'.$e.'"/>'.chr(10);

For tables just put </tr><tr> in place of the break.
Jonathon Reinhart
7 years ago
A very simple yet maybe not obvious use of the modulus (%) operator is to check if an integer is odd or even.
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.

for ($i = 1; $i <= 10; $i++) {
$i % 2) == 1//odd
{echo "<div class=\"dark\">$i</div>";}
{echo "<div class=\"light\">$i</div>";}
info at sima-pc dot com
10 years ago
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.
6 years ago
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:
/* tested under PHP 5.2.6-1 with Suhosin-Patch (cli) on both i386 and amd64, Debian lenny/sid */
$a = 2863311530;
$b = 256;
$c = $a % $b;
"$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).
calmarius at atw dot hu
6 years ago
Be careful when using % with large numbers.

The code:

echo 3333333333 % 3

puts out -1 instead of zero!

(Due to the overflow)
1 year ago
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.
pww8 at cornell dot edu
9 years ago
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.
glenn at benge dot co dot nz
9 years ago
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.
antickon at gmail dot com
2 years ago
not listed here is the absolutely useless unary plus.

= -3;
$a = +$a;
var_dump( $a ); // int(-3)
php at richardneill dot org
3 years ago
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.

(0xffffffff % 2);
//Prints  int(-1)   which is WRONG

//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)
hassan dot beydoun at live dot com
9 months ago

//using range function()
//$numbers = array;range = value

$numbers= range (1,100);
// foreach ($array as $value)
//{Do something with $value}
foreach ($numbers as $value)
"<p>This is number: $value</P>";}

To Top