From the course: Learning PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Figuring out even/odd with modulus

Figuring out even/odd with modulus - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Figuring out even/odd with modulus

- [Instructor] There's one more arithmetic operation that's a little different from the rest because it doesn't give the actual results, it gives the remainder of dividing two numbers and it's called modulus or modulo. It's represented by a present sign and it gives you the remainder of dividing two numbers. So if we have a gets two and b gets three and we echo b modulus a, that will print out one because when we divide three by two, we have a remainder of one. Similarly, if we were to echo 15 modulus nine, the remainder would be six. This is often used to find out if some number is odd or even. So if we write some code here where a gets five if a modulo two equals zero, then echo a is even else echo a is odd. Close my paragraph tag up here. If you divide any even number by two, the remainder will be zero. Now this was used for a long time programmatically to apply CSS classes to even table cells or even rows,…

Contents