views:

1111

answers:

3

What exactly does this mean?

$number = ( 3 - 2 + 7 ) % 7;
+9  A: 

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

Dana the Sane
+6  A: 

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 1 element each 3 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)

Pascal MARTIN
+4  A: 

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.

zombat
@zombat - 10 % 5 is 0 as 10 divided by 5 is 2 with a remainder of 0. Modulus is the remainder of the division operation.
Jeffrey Hines
yes, I'm having serious copy/paste issues here. Already caught that one :)
zombat
Thank you so much for putting it in terms I understood!
David