This is a short PHP tutorial on how to determine if a number is odd or even. This can be particularly useful if you need a PHP solution for alternating colors on HTML table rows.

## What are odd and even numbers?

I’ll try and keep these explanations short and sweet:

**Odd number**: A number that will leave a remainder if it is divided by 2. i.e. Ita multiple of 2.**is not****Even number**: A number thata multiple of 2. An even number will not leave a remainder if it is divided by 2.**is**

Take the following example:

If you divide 2 into 5, you are left with 2 and a remainder of 1. This means that **5 is an odd number**. However, if you divide 2 into 4, you are left with 2 and *no remainder*. This means that **4 is an even number**.

## Using PHP to determine whether a number is odd or even.

In PHP, we can use the **modulus operator** to determine if a number is odd or even. This particular operator will provide us with the remainder of a division sum. As a result, we can simply divide a number by 2 and then check its remainder to see if it is 0 or not.

Take a look at the following PHP example:

1 2 3 4 5 6 7 8 9 10 11 12 |
//The number that we want to check //is even or not. $number = 10; //Get the remainder of our number divided by 2. $remainder = $number % 2; //If the remainder is 0, then it means //that the number is even. if($remainder == 0){ echo $number . ' is even!'; } |

If you run the code above, you will see that 10 is an even number. This is because 10 divided by 2 does not leave a remainder.

Now, let’s specifically check to see if a number is an odd number:

1 2 3 4 5 6 7 8 9 10 11 |
//Our number. $number = 3; //Get the remainder. $remainder = $number % 2; //If the remainder is NOT 0, then //it is an odd number. if($remainder != 0){ echo $number . ' is odd!'; } |

The code above will output “3 is odd!” This is because dividing 2 into 3 leaves us with a remainder of 1.

Note that you can also shorten this code a little:

1 2 3 4 5 6 7 |
//Our number. $number = 3; //Shortened IF statement. if(($number % 2) != 0){ echo $number . ' is odd!'; } |

Now, let’s loop through the numbers 1-20 and output the results:

1 2 3 4 5 6 7 8 9 10 |
//foreach loop that loops through 1-20 foreach(range(1, 20) as $number){ //Check whether the number inside //our loop is odd or even. if(($number % 2) == 0){ echo "$number is even!<br>"; } else{ echo "$number is odd!<br>"; } } |

In the PHP above, we looped through the numbers 1-20 using the **range** function and printed out whether each number is odd or even.

## Alternating table row colors.

One of the most popular uses of this particular operation is to output table rows that alternate in color. This is because alternating table rows can make tables more user friendly and easier to read.

Let’s say that you a PHP array of pet types and that you want to output them into a HTML table:

1 2 3 4 5 6 7 8 9 10 |
//An array of pets that we want to //list in a HTML table. $pets = array( 'Dog', 'Cat', 'Hamster', 'Rabbit', 'Fish', 'Bearded Dragon' ); |

Now, let’s say that you want to alternate the color of each row in your table using a CSS class called “**stripped-row**“:

1 2 3 |
.stripped-row{ background-color: #eee; } |

Note that I have purposely kept the CSS class above as simple as it can be. You can obviously modify it to suit your own needs.

Now, let’s loop through our PHP array of pets and output them to HTML table that has alternate colored rows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<table> <?php foreach($pets as $index => $pet): $cssClass = ''; if(($index % 2) == 0){ $cssClass = ' class = "stripped-row"'; } ?> <tr <?= $cssClass; ?>> <td><?= $index; ?></td> <td><?= $pet; ?></td> </tr> <?php endforeach; ?> </table> |

In the PHP above, we used the modulus operator to get the remainder of the array index divided by 2. If the index of the current element is an **even number**, we apply our CSS class to the row.

If you run this particular example, you should get something that looks like this:

As you can see, all of the elements with an even number for an index have been given the “stripped-row” CSS class.

Hopefully, you found this tutorial useful!

## Facebook Comments