Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn’t even necessary and could be expressed as a one-liner like this:
Here’s the code to check the hours in the day starting with 0 (midnight) and ending at midnight (23):
Notes: The range function is handy for prefilling with a string or array with numbers. Now what if you want to show alternating messages, one on the even hours and one on odd hours? The following code will do just that if you change the print statement to be whatever you want to alternate:
Update 10/9/08 7:16am PST: Thanks to several commenters below who pointed out that there was a missing ‘)’ — it has been added now.

Very nice and quick lesson. Just what I was looking for
on January 9th, 2007 at 1:44 pm | #Link CommentThanks for this, a great quick answer - just what I needed
on November 28th, 2007 at 10:38 am | #Link CommentI tried this, but I couldnt get it to work. I used:
if($number & 1)
and
if($number & 0)
and it would be all screwy, sometimes work sometimes not. The code I got to work is:
if($number % 2)
to check for odd numbers, and
if(!($number % 2))
for even numbers. I like this better cause, well, I could get it to work, and also cause it’s intuitive. Divisable by 2? then even. Not divisible by 2? then odd.
on December 14th, 2007 at 2:54 pm | #Link CommentThanks, this is really useful. Been using it for some time. is_odd() helps with creating alternate table row classes too, for SQL results and such.
on December 20th, 2007 at 6:20 pm | #Link CommentYou missed an ) on the last example
on May 8th, 2008 at 1:03 pm | #Link CommentYou might want to consider changing this:
return $number & 1; // 0 = even, 1 = odd
to this:
return ($number & 1) ? true : false; // false = even, true = odd
Just for the sake of returning a pure boolean. Although it really shouldn’t matter in most cases.
on July 23rd, 2008 at 9:20 am | #Link Commentthere is a problem with your code. it won’t work you forgot a “)” it should have 3 of them after the H becuase you got to finish the date then the function then the if so it should look like this instead.
function is_odd($number) {
return $number & 1; // 0 = even, 1 = odd
}
if(is_odd(date(“H”))) {
on October 4th, 2008 at 2:39 am | #Link Commentprint “Since it’s an odd hour, I’ve got an odd message for you: read and be happy”;
} else {
print “It’s an even hour, Steven.”;
}
{You did|You probably did} {a great job|a fantastic job}.
Fantastic blog! I genuinely love how it’s easy on my small eyes along with the facts are well crafted. I’m wondering how I might be notified whenever a new post has been manufactured. I’ve subscribed to your rss feed which must do the secret! Posses…
on June 15th, 2011 at 5:10 am | #Link CommentGreate
It’s such a great site! http://www.zimbio.com/Mark+Scheifele/articles/XY1Z56mfJOF/NHL+Draft+Winnipeg+Jets+Choose+Mark+Scheifele Great post, I just bookmarked it on Digg.
on June 30th, 2011 at 5:30 am | #Link CommentGreat
love your blog, http://orgein.podbean.com/admin ,Thanks again.
on July 1st, 2011 at 9:38 am | #Link CommentNice
hello,very cool. http://akhluielre.blog.com/2011/07/07/bridesmaid-dresses-that-your-buddies-can-put-on-f/ ,nice to meet you
on July 7th, 2011 at 8:29 am | #Link CommentGreat One
I must say, its worth it! My link, http://donna.blogonsisters.com/,thanks haha
on August 9th, 2011 at 5:08 pm | #Link CommentGreat
love your blog, http://www.shadese.bloger.hr/ ,Thanks again.
on September 10th, 2011 at 7:04 am | #Link CommentGreat
Sport Studies: Why is study focused locally instead of internationally when sport is a natural phenomenon?, http://jtxyl.blog.stcn.com/
on October 21st, 2011 at 9:39 pm | #Link Comment< b >< a href=”http://just.45miles.de/DonnaRrbi” >bob haircuts< /a >< /b >< /blockquote >
Great post. I was checking constantly this blog and I’m impressed! Extremely helpful info particularly the last part :) I care for such information much. I was seeking this certain info for a long time. Thank you and good luck.
on February 5th, 2012 at 10:22 pm | #Link Comment