How to Use the Modulo Operator in PHP

PHP has a total of eight arithmetic operators. The most common are addition (+), subtraction (-), multiplication (*) and division (/). A slightly lesser-known, though still very important operator is modulo (%). In this tutorial, we'll focus on the modulo operator. We will discuss what it does and some of its practical uses.

What Does the Modulo Operator Do?

If you have two variables $a and $b, calculating $a % $b—usually pronounced "a modulo b" or "a mod b"—will give you the remainder after dividing $a by $b. Modulo is an integer operator, so  it converts both the operands to integers before calculating the remainder. So basically modulo does integer division and then gives back whatever is left from the dividend.

The sign of the value returned by a modulo operation is determined by the sign of the dividend. In division, the result after dividing two negative numbers will be a positive number. However, that's not the case with the modulo operator. The sign of divisor has no effect on the final value.

Here are a couple of examples:

Floating Point Modulo

If you want to calculate the remainder when two floating point numbers are divided by each other, you will have to use the fmod($dividend, $divisor) function. It returns the floating point remainder after the division. The remainder value will have the same sign as the dividend and its magnitude will be less than the divisor. The three numbers are related by following relation:

Here, the value i will always be an integer.

You should remember that floating point arithmetic is not always accurate due to limitations of the binary or decimal representation of fractions. For example, 1/3 cannot be accurately represented in decimal form. You can keep writing 0.33333.... but at some point you would have to stop. You will get closer to the original value with each additional 3 in the decimal representation but the value will still not be exactly 1/3.

This kind of inaccuracy causes problems with the fmod() function: the results are not entirely reliable.

Here are some examples of the fmod() function:

The second value isn't accurate because 0.2 divides 18.8 perfectly. This is just a shortcoming of calculations in the floating point format used by computers.

Uses of the Modulo Operator

In this tutorial, we will restrict ourselves to integer modulo because it is much more common and has a lot of applications.

Checking if a Number is Multiple of Some Other Number

The result of the modulo operator is zero if the first number is perfectly divisible by the second number. This could be used to check if one number is multiple of the other in a given number pair. Probably the most common use of this property of the modulo operator is in checking if a number is even or odd. Here is an example:

In the above example, you could be getting the list of colors from some user and ask them to only provide even number of colors.

The example below uses a similar reasoning to create groups with 5 students each. In real life, you will have to use extra code to group the students but the basic idea of checking if the total students are multiples of 5 does not change.

Changing Numbers to be Multiple of Some Other Number

In the above section, we used the modulo operator to inform users to only provide input values in certain multiple. If that is not possible, we can also force the input to be even as well as a multiple of 5 or some other number.

The modulo operator provides the whole number left after dividing the first number with the second number. This means that subtracting the remainder from the first number will make it a multiple of the second number. For example, 28 can be changed to be multiple of 5 by taking the modulo 28 % 5. In this case, the modulo will be 3. We can now subtract 3 from the original number to make it a multiple of 5. The following line will force any positive number x to be a multiple of some other positive number y by subtracting an appropriate value from it.

In our previous example with 28 students, we could just leave 3 students out and group other students together.

Put a Limit on the Input

As I mentioned in the beginning of the post, in case of positive numbers the modulo operator will return a number between 0 and N - 1, where N is the divisor. This means that you can put a cap on any input and do some operations repetitively and sequentially. Here is an example:

In the above example, we have just five colors but a total of 180 images. This means that we will have to keep looping through the same five colors and assign them to all our images. The modulo operator fits this need perfectly. It will restrict the value of $i % $color_count between 0 and (5 - 1) or 4 inclusive. In other words, we will be able to pick all colors of our array sequentially very easily.

Do Some Task Every Nth Time in a Loop

When traversing a loop, we can check the value of a variable incremented with each pass through the loop and do some specific task after every nth iteration. One practical use case that comes to mind is updating users about some long-running process. Lets say you are making changes to 1000 different images using PHP. If the changes are significant, this process will take a while to update all images.

In such cases, the users will have no way of knowing if the program is just stuck or actually making any progress. What you could do is report the progress to users after editing every 10th image.

The update_images() function in the above example is completely made up but you could replace it with some other processes like resizing the images, adding watermark, turning them gray scale etc. (Check out my PHP GD image editing tutorials if you want to learn how to programatically edit images in PHP yourself.)

Converting Between Different Units of Measurement

The modulo operator can also be used to convert between different units of measurement. For example, you could use it to change a time duration expressed in number of seconds into the same duration expressed in hours, minutes and seconds. Similarly, you could also convert a large of centimeters into kilometers, meters and centimeters. Here is an example:

We begin by simply dividing the total number of seconds by 3600 and casting the value into an integer. This gives us the total number of hours since every hour has 3600 seconds. In the next step, we subtract 3600 * $hours from the original number of seconds. This gets rid of all the seconds that we have converted to hours. Dividing by 60 now will give us the total number of minutes. Finally, we use the modulo operator to get the number of seconds.

Final Thoughts

As you saw in this tutorial, the modulo operator, though easy to use, has a lot of applications. We began this tutorial by looking at the modulo of both positive and negative numbers as well as floats. After that we covered some common scenarios where we would use modulo. 

If you have any questions related to this tutorial, please let me know in the comments. Do you have any other uses of modulo in mind? Please share them with fellow readers by posting them below.



from Envato Tuts+ Tutorials

Comments

Popular posts from this blog

How to Build Self-Confidence: 10+ Steps to Improve Your Self-Esteem

Best of 2020: Great Landing Pages for Design Inspiration (Showcase Examples)

18+ Best Free PowerPoint Puzzle Pieces Templates (Infographic PPT Slides)