Easy questions about odds and evens
Moderator: General Moderators
Easy questions about odds and evens
Can someone point me in the direction of checking a value for odd or even?
Thanks
Rand
Thanks
Rand
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
Code: Select all
if ($num % 2 == 0) {
//number is even
}else{
//number is odd
}- Michael 01
- Forum Commoner
- Posts: 87
- Joined: Wed Feb 04, 2004 12:26 am
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){
if($numvalue & 1) return "odd";
else return "even";
}
?>If ever 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
Code: Select all
$num % 2give 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