What is PHP modulus operator?

we can say that modulus is the division reminder of the operation.

In this below example the remainder (the modulus, or technically, the modulo) of 5 divided by 2 is 1, it calculating by checking how much 2 in the 5 and the balance(reminder) will show.

  $a = 5;
  $b = 2;
  $c = $a % $2;
  echo $c; // Output 1

In this more real-world example, the function find() will be called whenever the variable $count divided by 1,000 is zero:


  // some code up here ...

  // php modulus operator
  if ($count % 1000 == 0)

  // more code down here ...


I do something like this when I generate the sitemap files for this website. I try to keep each sitemap file at no more than 1,000 URLs, so I use code like this to stop writing one file, and then start writing the next file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.