Contact Us

Understanding Modulus in PHP

The concept of Modulus isn’t that difficult to understand however without proper explanation it can be hard to get your head around at first read of the simple explanation given in the PHP manual, as shown below:

Remainder of $a divided by $b.

Thankfully the modulus arithmetic operator really boils down to basic high school maths at its heart and is quite easy to understand once you know what it does.

The symbol used for modulus is the percentage symbol (%) and is part of the group of PHP arithmetic operators which includes – (negation and subtraction), +, *, / and % which can be found here.

Taking the example of 7 % 5, this would simply calculate how many times 5 can fit in to the number 7 and then leave us with the remaining number, so 5 goes into 7 once with 2 left over meaning that the following statement would return 2.

echo 7 % 5;

So what if we reverse this and use 5 % 7 instead?

Well in this example our script will return the value 5 as 7 doesn’t go into 5 at all due to it being too big a number and as such returns the remaining 5.

Ok, so why would you ever use something like this? Well, one of the best uses is for finding our if a number is odd or even and then styling rows accordingly. Consider the following example:

for ($x = 0; $data = mysql_fetch_object($result); $x++)
{
$color = $x % 2 == 0 ? 'white' : 'grey';

This would loop through an object and would calculate to see if each value of $x has a remainder or not once divided by 2.  You should be aware from your school days that any number that is perfectly divisible by 2 has no remainder.

There are of course more advanced uses for this operator but for now this should give you enough to explore the function more deeply.

If you would like any help with your web development then why not get in contact with us here at Network Intellect.

This article was posted by



Leave a Reply

What our clients say

"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
"They have always been professional and focused on delivering results in a timely and cost effective manner. It is enlightening to have an organisation that takes the time to make recommendations on actual data analysis."
Business Manager, Charity
"They (Network Intellect) continue to exhibit their digital expertise by helping us to establish a competitive advantage by pushing the boundaries and innovating their technology and performance solutions."
Head of Marketing Communications, Education
"Because of their talent, expertise and level of service our on-line business has increased dramatically."
Founder, Electronics
"I would have no hesitation in recommending them as an analytics partner to understand and improve the performance of a website."
Vice President, Retail
Network Intellect continue to give us a fantastic service, fast and friendly and are always ready to go the extra mile. They have gone above and beyond the call of duty to deliver our results.
Director, E-Cigarette Web
"We found that Network Intellect had the depth of knowledge we needed and after speaking to them a few times we decided to go with them. It’s the best decision we’ve made in some time."
Owner, Retail
"Their services offered great value for money. I would not hesitate to recommend them as a digital partner to others."
Strategic Marketing Manager, Education
"Jason and his team at Network Intellect embody efficiency and efficacy. I would have no hesitation in recommending this excellent company to others."
Marketing Manager, Education
Network Intellect have managed our product data feeds with exceptional care and meticulous detail. I know I can trust them to make the right decisions for our business."
Director, E-Commerce
"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
Read all testimonialsRead all testimonials

We can help you be great

We have friendly staff ready to help you get on your path to greatness...
Network Intellect Menu