What does the percent sign mean in PHP?
Asked Answered
L

6

45

What exactly does this mean?

$number = ( 3 - 2 + 7 ) % 7;
Liberal answered 19/12, 2009 at 21:21 Comment(0)
H
82

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.

Hoffert answered 19/12, 2009 at 21:23 Comment(1)
@Hoffert - 10 % 5 is 0 as 10 divided by 5 is 2 with a remainder of 0. Modulus is the remainder of the division operation.Gnosis
B
22

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.)

Braynard answered 19/12, 2009 at 21:22 Comment(0)
O
12

It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b

Outsider answered 19/12, 2009 at 21:21 Comment(0)
H
4

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'; ?>
Hypocycloid answered 30/4, 2012 at 10:49 Comment(0)
B
3

% 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.

Bul answered 12/7, 2014 at 11:46 Comment(0)
B
3

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)

Bumbailiff answered 21/10, 2014 at 19:19 Comment(2)
Since you are showing non integer math it should be mentioned that the function in PHP for floating point modulus is fmod()Gabriella
@Gabriella that's outside of the scope of this question 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.