**Foreword**: There comes the time in every web developer’s escapades that he or she needs to find an odd or even number. Perhaps for use in alternating color schemes in blog comments, or even for more serious applications such as error checking. Whatever the case may be, we can write up a simple function or IF statement to do the hard work for us.

### Find An Odd Or Even Number With The IF Statement

We can check for whether or not a number is even or odd by using the modulus operator seen below.

```
<?php
if (7 % 2) {
echo "7 is odd";
} else {
echo "7 is even";
}
?>
```

We interpret the first line as “Divide 7 by two, and check for a remainder.” If there is a remainder (in our case we have 3.5, so a remainder of 0.5), then the number is odd. If there isn’t a remainder while dividing by two, then that number is even.

You could change the three sevens in the above script to eights, and you will notice the script outputs the number is even, since 8 divided by two is 4 with no remainder.

### Checking For Odd And Even Numbers With A PHP Function

We can clean up so code here by using a function to do the dirty work for us. First we’ll stick the if statement into a function and make sure it works, then we’ll optimize our code once we get the concept up and running.

```
<?php
function is_odd($num) {
if ($num % 2 == 0) {
return false;
} else {
return true;
}
}
############################
if (is_odd(4)) {
echo "This number is odd.";
} else {
echo "This number is even.";
}
?>
```

The function will do exactly what we just did with our IF statement previously, only this time we can reuse our code and save quite a bit of space. We supplied the **is_odd** function with a plain number; if we wanted we could put in a variable. In fact, we’ll do just that while we optimize our code in the next example.

```
<?php
function is_odd($num){
return $num % 2 == 0 ? false:true;
}
$number_To_Check = 3;
if (is_odd($number_To_Check)) {
echo "$number_To_Check is odd.";
} else {
echo "$number_To_Check is even.";
}
?>
```

This time around we are saving even more space by using the ternary symbol (?). In this instance the left side of the colon is what gets executed if the statement is true, and the right side is what gets executed if the statement is false. Think of the colon as an “else” statement.We can use some pseudo-code to help understand this more:

```
<?php
function odd_check($num){
(Is this statement true) ? yes : no;
}
?>
```

Back to our case example: since the statement checks for an even number, when the statement is true we return the value “false” to indicate it isn’t an odd number.

### Further Optimization of The Odd Number-Finding Function

Want to really impress the ladies? Try showing them the following function to find odd and even numbers.

```
<?php
function is_odd( $num)
{
return( $num & 1 );
}
########################
if (is_odd(7)) {
echo "7 is odd";
} else {
echo "7 is even";
}
?>
```

This time around we took the equation out. We can do this through using the AND operator (&). If you aren’t up to speed on binary, consult the chart below to see how we count to five. (Note that we will not go into depth on binary, just give you enough information to understand the AND process.)

Each place in the above numbers represents a different value. The furthest right number, as you might have guessed represents “1,” the next “2,” the next “4,” and so on. But notice something even more interesting: all the odd numbers have a “1″ while the even numbers don’t!

The AND operator is going to compare two binary numbers. If both numbers have a “true” (the number 1) value, then the result is true. If either numbers have a 0 (or “false”), or both numbers are false, the resulting number is 0. Consult the diagram below for more information.

Back to our equation. If the ($int & 1) check returns true, the number is odd. Let’s say we put an odd number such as 3 in. We will compare it against 1, which is already an odd number. 0011 and 0001 can be used in an AND equation to get the result of 1, since both values are 1 (and thus true).

You don’t necessarily have to understand this jargon to make use of the function- but it’s good to get the basics of binary down if you haven’t already.

#### Closing Comments

Now that you can find odd and even numbers, you can accomplish all types of artsy things. You’ll see this in effect most often on blogs that alternate the background colors of comments to make for easy reading. Lyrics websites do the same thing, and you can even use the same principle to use in more complex equations (which we won’t get into in the scope of this article).

Try running the following code just for fun:

```
<?php
function is_odd($num)
{
return( $num& 1 );
}
for ($counter = 0; $counter <= 20; $counter++) {
if (is_odd($num)) {
echo "<div style='background-color:black;width:800px;height:30px;margin:0px auto;'> </div>";
} else {
echo "<div style='background-color:red;width:800px;height:30px;margin:0px auto;'> </div>";
}
}
?>
```

**Your Name**

**Your Email Address**

**Your Comment**

June 22, 2009hi

thankx sir i am able to learn php in easier way.

Leave a replyJuly 28, 2009First a complaint, then a compliment. 7 % 2 = 1, not .5; You’re confusing division with modulo (7 / 2 = 3.5). With 7 modulo 2, 2 goes into 7, 3 complete times, with a remainder of 1. Try it: echo (7%2); Also, i’ve never seen someone explain binary quite as well, your graphics were very helpful in this purpose. Good job.

Leave a replyAugust 2, 2009hi – you need you use the variable $counter instead of $num on the if statement inside the for-loop.

“if (is_odd($counter)) {“

Leave a replyAugust 16, 2009Hi there, Can you provide a server to execute these codes ?

Leave a replyNovember 22, 2009ur details r very good….

Leave a replyJanuary 13, 2010i want to use a function and inside that function i have to use conditional operator so that i can get the even number that are divided by 2 with a remainder 0.

i have done but i can’t get the exact output in echo command.

can anyone advise me?

Leave a replyFebruary 17, 2010oh thanks

Leave a replyMay 15, 2010Its easy but if you provide some visual lesson then it will be more convenient for us

Leave a replyOctober 7, 2010Thanks so much for the easy explaining =) very helpful

Leave a replyJune 30, 2011how to get a series of numbers in ascending order.

For Ex- $a = array(12, 34, 1, 45, 7, 8, 2, 356, 100, 113, 78, 13);

my question is that, how can i get it in this following manner

$a = 1, 2, 7, 8, 12, 13, 34, 45, 78, 100, 113, 356

like that.

Leave a replyAugust 29, 2011using PHP’s sort function

PS: I don’t know the reason why i can’t post full code

Leave a replyNovember 18, 2013How would you Display even numbers ?

<?php

/* Display even number between 30 and 40 (30, 32, 34, 36, 38, 40) */

for ($i = 30; $i <= 40; $i++) {

echo $i;

}

Leave a reply