What is modulo operator in php?
Remember basic arithmetic from school? These work just like those. Show
The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned. For integer division, see intdiv(). Operands of modulo are converted to int before processing. For floating-point modulo, see fmod(). The result of the modulo operator
There are no user contributed notes for this page. ❮ PHP Math Reference ExampleReturn the remainder of x/y: echo(fmod(20, 4) . " Try it Yourself » Definition and UsageThe fmod() function returns the remainder (modulo) of x/y. SyntaxParameter Values
Technical Details
❮ PHP Math Reference When I learned math, I didn’t really understand the modulus function. In fact, it was until I learned PHP that I understood the value of a remainder from division. PHP’s role in making websites makes the modulus operator (or modulo) much more important than it ever was in math class for me. In this article, we’ll cover the PHP modulo operator, the mathematics behind it, and why using it for things like finding every 4th item and more are just so darn useful when you’re aiming to make great web pages. The Core Concept: What Modular Division MeansWhen I was in school (I do imagine it’s different now) we learned division via a many-step process. I won’t bother to explain it in text, but sometime division is round: we know that 21/3 = 7. And sometimes it’s not. 22/3 = 7.???????. That’s how this whole “modulus”, “modulo”, or “modular division” comes to be a little important if you’re doing PHP.Mod in PHP is useful become sometimes it’s very useful to know that the “remainder” when you divide 22 by 3 is one. Which is to say that you can clearly get at least 7 out of dividing 22 by 3, and that one “uneven” bit remains after you’ve done this. This is useful for a few reasons. First because you may know off-hand (that is: without having to think hard) that 1/3 is expressed decimally as .33333. So I know that 22/3 = 7.33333. Because I know that 7 fits cleanly, and that I could express the remainder of my division cleanly as 1, or 1/3 = 0.33333. What PHP Modulo Does For UsSo, these remainders. They matter a little more than just the ability to more easily find decimal fractions of numbers. How else do they matter? Primarily in that we’re able to consistently know that we have the “second of every three” items simply from knowing the remainder of dividing. What does this allow us to do? It always us to use a PHP remainder function to cleanly label a whole set of things (say I’m getting a bit ahead of myself though. First let’s make sure we understand how we write out a PHP modulo operation. Then we can get into what knowing the remainder unlocks for us in our PHP code. Basic Calculation in using the Modulus Operator in PHPSo, I’ve told you about this magic “get the remainder” PHP functionality. It’s one of PHP’s many arithmetic operations. How do you use it? I won’t belabor it too long. Here’s how you get the remainder we discussed above for dividing 22 by 3:
If you didn’t catch it look again.
That’s not the division symbol So like I said before, what’s powerful about the modulus operator (the humble
This PHP code works to find if a number is odd. Determining odd or even in PHP code is that simple. More accurately, it’s simple if you understand the modulo operator. Which hopefully you’re well on your way to. Technically, this works because PHP does type juggling. When I divide an even numbers by 2, there is never any remainder. So Using PHP Modulus: Every 3 Items Gets HighlightedIf I know that I’ll only get two values when something is modulo 2, I also know that I’ll only get three values for
We discussed that anything What all this means is that it’s pretty easy for us to highlight every third thing in PHP. Here’s a quick example:
|