Page 1 of 1

Easy questions about odds and evens

Posted: Mon Feb 16, 2004 5:56 pm
by kanshou
Can someone point me in the direction of checking a value for odd or even?
Thanks
Rand

Posted: Mon Feb 16, 2004 6:02 pm
by Illusionist

Code: Select all

if ($num % 2 == 0) {
    //number is even
}else{
    //number is odd
}

Posted: Mon Feb 16, 2004 6:10 pm
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;
?>

Posted: Mon Feb 16, 2004 6:12 pm
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

Posted: Mon Feb 16, 2004 7:29 pm
by Illusionist
yeah, your right, but no one asked what it meant... ?!?!

Posted: Mon Feb 16, 2004 9:33 pm
by kanshou
Thanks for the help folks, I appreciate it! 8)

Posted: Tue Feb 17, 2004 2:05 am
by Dr Evil
Illusionist wrote:yeah, your right, but no one asked what it meant... ?!?!
Do I feel some kind of agressivity ?

Posted: Tue Feb 17, 2004 3:39 am
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.