6

What is the explanation for PHP's operator % in full detail?

Including examples would be nice!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
foo
  • 63
  • 1
  • 3

9 Answers9

17

It's the modulus operator, which gives the integer remainder of a division e.g.

7 / 2 = 3.5  // 3 remainder 1
7 % 2 = 1    // the remainder

Obvious real world example is working out whether a number is odd or even

if (($n % 2) == 0) the number is even, else it's odd... useful when you want to show alternate rows in a table in different colours

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • It's not the fractional part of the division, it's the remainder, so `7 % 2` is `1`, not `.5` – Michael Mrozek Jul 08 '10 at 15:02
  • Your second example should be 7 % 2 = 1 because 7 / 2 = 3 with remainder 1. – murgatroid99 Jul 08 '10 at 15:02
  • 5
    FYI for anyone reading this 6 years later, you should not use `$n % 2` to give your table rows different classes, use the CSS `:even` and `:odd` selectors instead. –  Feb 23 '16 at 08:53
1

% is the modulus operator.

An example

$num1 = 160;
$num2 = 15;
$result = $num1 % $num2;
echo "The modulus of these numbers is $result";
Community
  • 1
  • 1
osm
  • 4,186
  • 3
  • 23
  • 24
0

By example, % may be used to set an additional CSS class for every third element in HTML:

for ($i = 1; $i <= 30; $i++) {
    $additionalCssClass = ($i % 3 == 0 ) ? ' last' : '';
    ?><div class="catalogItem<?php echo $additionalCssClass;?>">&nbsp;</div><?
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrey Vorobyev
  • 896
  • 1
  • 10
  • 37
0

% is used for the remainder.

Example:

Print if a number is even or odd

  (@num % 2 == 0 )? 'even' : 'odd'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salil
  • 46,566
  • 21
  • 122
  • 156
0

It's the modulus operator. It gives you the "remainder" after a division. It's a fairly standard operator.

Here is the PHP reference for Arithmetic Operators.

rtalbot
  • 1,615
  • 10
  • 13
0

It will give you the modulo, or "mod", of two numbers, which is the remainder when you divide two numbers. It's a common arithmetic operator, and I can't think of a language that doesn't have it. More information is in Modulo operation.

There are two ways that you can use it. The most common is like any other arithmetic operator:

$bwah = 3 % 1; // == 0
$bwah = 10 % 3; // == 1

There is also a shorthand way of doing it, just like +=, -=, *=, and /=:

$bwah = 10;
$bwah %= 3; // == 1 ... it's like saying 10 % 3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Bisbee
  • 4,461
  • 20
  • 25
0

Just a use of the % modulus operator:

       if($nextImage == $ImagesTotal){
           //reset counting
           $nextImage = 0;
       } else {
          //increase counting
           $nextImage++;
       }

can be simplified to:

       $nextImage = ++$nextImage % $ImagesTotal;

        //$nextImage will allways be a value between 0 and $ImagesTotal;
ion
  • 540
  • 7
  • 20
0

As a real-word problem I use it all over for generating HTML, especially tables:

//Rows
for ($i=0; $i<30; $i++) {

    if ($i%3 == 0)
        echo('&lt;tr&gt;');

    echo('&lt;td&gt;'.$i.'&lt;/td&gt;');

    if ($i%3 == 2)
        echo('&lt;/tr&gt;');
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Imran Naqvi
  • 2,202
  • 5
  • 26
  • 53
0
$length = 38;
$feet = floor($length / 12);
$inches = $length % 12;
echo 'This is '.$feet.' feet and '.$inches.' inches';

Prints This is 3 feet and 2 inches

Wally Kolcz
  • 1,604
  • 3
  • 24
  • 45