What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5
returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10
returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5
returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7)
works out to 8, giving you 8 % 7
, so $number
will be 1
, which is the remainder of 8/7.
It is the modulus operator:
$a % $b
= Remainder of$a
divided by$b
.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3
would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b
the result is the remainder when $a
is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
%
means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r
is for the remainder (answer), $n
for the numerator and $d
for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So %
(integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1
:
It can be broken down into:
(3 - 2 + 7) = 8 8 / 7 = 1.143 #Rounded up .143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of
21 % 3 = 0
:21 / 3 = 7.0 3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of
21 % 6 = 3
:21 / 6 = 3.5 .5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of
21 % 8 = 5
:21 / 8 = 2.625 .625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)
What does % mean?
, since we're only talking about %
(Integer modulus). Additionally my answer only demonstrates how modulus is being determined, so that the math being performed is understood. Not performing modulus on a float
values where fmod()
would be needed. –
Bumbailiff © 2022 - 2024 — McMap. All rights reserved.