Arithmetic Operators
Remember basic arithmetic from school? These work just like those.
Example | Name | Result |
---|---|---|
-$a | Negation | Opposite of $a. |
$a + $b | Addition | Sum of $a and $b. |
$a - $b | Subtraction | Difference of $a and $b. |
$a * $b | Multiplication | Product of $a and $b. |
$a / $b | Division | Quotient of $a and $b. |
$a % $b | Modulus | Remainder of $a divided by $b. |
The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned.
Operands of modulus are converted to integers (by stripping the decimal part) before processing.
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:
<?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
?>
See also the manual page on Math functions.
Коментарии
It is time for PHP to distinguish between the Reminder and the Modulus and define them correctly mathematically.
Let us assume that we have two integers a and b. Then, the reminder (denoted by r) of a (the dividend) divided by b (the divisor) should merely satisfy the equation a = q*b + r, where q is the quotient.
In case of modulus, however, it refers to a finite set originated by b, that is {0, 1, 2, ..., b-1}. The result of the modulus operation must be an element of that finite set.
To illustrate, let us assume that a = 5, and b = 3. This means we have a finite set {0, 1, 2}. The modulus operation, a mod b or 5 mod 3, must return 2. Think about it as a loop on the elements of the set for a times, in this case 5 times.
If a = 0, then it returns 0.
If a = 1, then it returns 1.
If a = 2, then it returns 2.
If a = 3, then it returns 0.
If a = 4, then it returns 1.
If a = 5, then it returns 2.
And so on so forth. It is forward loop.
If a is negative, then it means backward or reverse loop.
If a = -1, then it returns 2.
If a = -2, then it returns 1.
If a = -3, then it returns 0.
If a = -4, then it returns 2.
If a = -5, then it returns 1.
And so on so forth.
If b is negative, then the set is constructed as follows {0, b+1, b+2, b+3, ..., -1}. So, if b = -4, then the set is {0, -3, -2, -1}. a is still the circular index of the loop, so:
If a is positive, then forward loop:
If a = 0, then it returns 0.
If a = 1, then it returns -3.
If a = 2, then it returns -2.
If a = 3, then it returns -1.
If a = 4, then it returns 0.
If a = 5, then it returns -3.
If a = 6, then it returns -2.
If a = 7, then it returns -1.
If a = 8, then it returns 0.
And so on so forth.
If a is negative, then reverse loop.
If a = -1, then it returns -1.
If a = -2, then it returns -2.
If a = -3, then it returns -3.
If a = -4, then it returns 0.
If a = -5, then it returns -1.
If a = -6, then it returns -2.
If a = -7, then it returns -3.
If a = -8, then it returns 0.
If a = -9, then it returns -1.
And so on so forth.
Therefore, the result of the modulo operator % has the same sign as the divisor, not the dividend. And the reminder has the same sign as the dividend. Moreover, though it is good to mind the sign, minding the results is far more important.
The table below summaries the differences:
<?php
a | b | a mod b | a rem b
------+-----+-----------+------------
13 | 4 | 1 | 1
------+-----+-----------+------------
13 | -4 | -3 | 1
------+-----+-----------+------------
-13 | 4 | 3 | -1
------+-----+-----------+------------
-13 | -4 | -1 | -1
------+-----+-----------+------------
?>