The PHP modulo is an arithmetic operator which returns the remainder or a modulo of a number.
In this article, we will talk about Modulo operator in a detailed explanation as well as example programs that could be helpful to your future development. This topic is a continuation of the previous topic, entitled Isset in PHP Function.
What is PHP modulo?
In PHP, modulo is an integer operator which is used to find out the remainder (
modulo) after the division of an integer of two integers. Primarily, the results always show up the remainder in an integer form.
<?php $sample1 = 2091 % 37; echo $sample1."\n"; // Output: 19 $sample2 = 43089 % -76; echo $sample2."\n"; // Output: 73 $sample3 = -2049 % 38; echo $sample3."\n"; // Output: -35 $sample4 = -2134 % -30; echo $sample4."\n"; // Output: -4 $sample5 = -47.4 % -3.2; echo $sample5."\n"; // Output: -2 $sample6 = -66.9 % -5.7; echo $sample6."\n"; // Output: -1 $sample7 = -87 % -9; echo $sample7; // Output: -3 ?>
19 73 -35 -4 -2 -1 -6
What does the modulo operator (%) do?
The modulo operator is a modulo-division operator which use to produce the remainder of an integer division. And it is symbolized by (%) and is part of 8 arithmetic operators in PHP.
<?Php echo 10.5 % 3; ?>
How does PHP calculate the remainder?
To calculate a remainder in PHP there is also a function name fmod() to get the remainder of a (modulo) of an x and y.
<?php echo fmod(10.5, 3); ?>
Why do we need modulo?
The modulo is needed because it produces a remainder for an integer division and that remainder is always an integer number only
What is modulo in simple terms?
The modulo in simple mean is a coefficient or a constant that usually expresses a numerical degree to which the substance of a body possesses a property as elasticity.
- Types of Operators In PHP (With Examples)
- PHP Shorthand If ElseIf Else
- PHP Spread Operator (with Advanced Program Examples)
- PHP Elvis Operator (Detailed Explanation With Examples)
- PHP Coding Standards with Best Example
In summary, you have learned about PHP Modulo operator. This article also discussed what PHP modulo is, what the modulo operator does, how PHP calculates the remainder, why we need modulo, and what is modulo in simple terms.
I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.