Easy questions about odds and evens

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kanshou
Forum Commoner
Posts: 47
Joined: Sun Aug 03, 2003 1:57 pm
Location: San Diego
Contact:

Easy questions about odds and evens

Post by kanshou »

Can someone point me in the direction of checking a value for odd or even?
Thanks
Rand
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Code: Select all

if ($num % 2 == 0) {
    //number is even
}else{
    //number is odd
}
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

I got this one off the php.net site. the possiblities of having a overun on numbers when dividing is possible without a floor() function I think.....not sure, but this one seems to work pretty good to.

Code: Select all

<?php
function oddeven($numvalue)&#123;
 if($numvalue & 1) return "odd";
 else return "even";
&#125;
?>
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

If ever

Code: Select all

$num % 2
means value modulo 2 or in english :
give me the leftover from a division by 2.
If it is 0 then the number is even.

Just my $0.000002
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yeah, your right, but no one asked what it meant... ?!?!
User avatar
kanshou
Forum Commoner
Posts: 47
Joined: Sun Aug 03, 2003 1:57 pm
Location: San Diego
Contact:

Post by kanshou »

Thanks for the help folks, I appreciate it! 8)
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Illusionist wrote:yeah, your right, but no one asked what it meant... ?!?!
Do I feel some kind of agressivity ?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Dr Evil wrote:
Illusionist wrote:yeah, your right, but no one asked what it meant... ?!?!
Do I feel some kind of agressivity ?
Dr Evil, I feel our young friend needs a cup of tea to learn to relax a bit.
Post Reply