How can i get mod in php?
PHP has a total of eight arithmetic operators. The most common are addition ( Show
What Does the Modulo Operator Do?If you have two variables 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 the divisor has no effect on the final value. Here are a couple of examples: Floating-Point ModuloIf you want to calculate the remainder when two floating-point numbers are divided by each other, you will have to use
the $dividend = i*$divisor + $remainder Here, the value You should remember that floating-point arithmetic is not always accurate due to the 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 Here are some examples
of the The second value isn't accurate because 0.2 divides into 18.8 perfectly. This is just a shortcoming of calculations in the floating-point format used by computers. Arbitrary Precision ModulusPHP also has functions for calculating the Modulus for arbitrary precision numbers using the Uses of the Modulo OperatorIn 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 a Multiple of Some Other NumberThe 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 a 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 a user and asking them to only provide an 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 a Multiple of Some Other NumberIn the above section, we used the modulo operator to tell users only to provide input values in certain multiples. 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 by 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 a 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 = x - (x % y) In our previous example with 28 students, we could just leave 3 students out and group the other students together. Put a Limit on the InputAs I mentioned at the beginning of the post, in the 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 Do Some Task Every Nth Time in a LoopWhen traversing a loop, we can check the value of a variable incremented with each pass through the loop and perform a specific task after every nth iteration. One practical use case that comes to mind is updating users about a long-running process. Let's say you're making changes to 1,000 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 Converting Between Different Units of MeasurementThe 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 seconds into the same duration expressed in hours, minutes, and seconds. Similarly, you could also convert a large number of centimeters into kilometers, meters, and centimeters. Here is an example: We begin by simply dividing the total number of seconds by 3,600 and casting the value into an integer. This gives us the total number of hours since every hour has 3,600 seconds. In the next step, we subtract Calculating the GCD of Two NumbersYou can also use the modulo operator to quickly calculate the greatest common divisor (GCD) of two numbers. The trick is to keep calculating the modulo of the numbers and reassigning them until they divide completely. We keep calculating Calculating Modulus With Modular ExponentiationModular exponentation is used in many fields in computer science like cryptography. It relies on a property of modulus where: (a ⋅ b) mod m = [(a mod m) ⋅ (b mod m)] mod m We can create a function based on the above property to calcuate the remainder when dividing x y by m. Calculating the modulo by first evaluating the exponent 44 123 would have given us a 203-digit number. It would be much bigger if the exponent is over 1,000. In such cases, using the above function will give us the modulo much more quickly. The function simply returns 0
if the divisor is 1. Otherwise, it runs a for loop for Final ThoughtsAs 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. Learn PHP With a Free Online CourseIf you want to learn PHP, check out our free online course on PHP fundamentals! In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database. Did you find this post useful? Freelancer, Instructor I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around. How do I mod PHP?For integer division, see intdiv(). Operands of modulo are converted to int before processing. For floating-point modulo, see fmod().
...
Arithmetic Operators.. How can I get modulus of a number in PHP?The fmod() function returns the remainder (modulo) of x/y.
What does modulus operator do in PHP?The modulo operator provides the whole number left after dividing the first number by 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 a multiple of 5 by taking the modulo 28 % 5.
|