RE: [PHP] Is it odd or even???
> Is there an easy way to check and see if a number is odd or even? if( $num % 2 ) { echo "Odd"; } else { echo "Even"; } Chris
Re: [PHP] Is it odd or even???
You could divide it by 2, odd numbers will always have a remainder even umbers never will. here are math functions, but none for odd/even that i see. Jon - Original Message - From: "Brandon Orther" <[EMAIL PROTECTED]> To: "PHP User Group" <[EMAIL PROTECTED]> Sent: Monday, March 05, 2001 3:17 PM Subject: [PHP] Is it odd or even??? > Hello, > > Is there an easy way to check and see if a number is odd or even? > > Thank you, > > > Brandon Orther > WebIntellects Design/Development Manager > [EMAIL PROTECTED] > 800-994-6364 > www.webintellects.com > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Is it odd or even???
use the modulus operator: ($number % 2) returns 0 if the value is even Larry Jeannette MIS Director, e.Republic -Original Message- From: Brandon Orther [mailto:[EMAIL PROTECTED]] Sent: Monday, March 05, 2001 12:18 PM To: PHP User Group Subject: [PHP] Is it odd or even??? Hello, Is there an easy way to check and see if a number is odd or even? Thank you, Brandon Orther WebIntellects Design/Development Manager [EMAIL PROTECTED] 800-994-6364 www.webintellects.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Is it odd or even???
> That won't work. % returns the remainder from a division of > the number divided by the mod number. Yours: > if($num %2 == 0){ > echo "even"; > }else{ > echo "odd"; > } Mine: > > if( $num % 2 ) { > > echo "Odd"; > > > > } else { > > echo "Even"; > > > > } Ours are identical, just the other way around. This: if( $num % 2 ) { means if the result of that operation is anything other than 0 (zero), which is a truth in PHP. Chris
RE: [PHP] Is it odd or even???
Use the modulus operator > -Original Message- > From: Brandon Orther [mailto:[EMAIL PROTECTED]] > Sent: Monday, March 05, 2001 3:18 PM > To: PHP User Group > Subject: [PHP] Is it odd or even??? > > > Hello, > > Is there an easy way to check and see if a number is odd or even? > > Thank you, > > > Brandon Orther > WebIntellects Design/Development Manager > [EMAIL PROTECTED] > 800-994-6364 > www.webintellects.com > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Is it odd or even???
Color me confused because I though true was any non zero value and false was zero. I know I am using the following code: if (!($num % 4){ do something} and it does something when $num is evenly divisible by 4 (ie. $num % 4 = 0). If I were testing for odd vs even I'd do the following: if (!($num % 2) { Odd } else { Even } John Guynn This email brought to you by RFCs 821 and 1225. -Original Message- From: Boget, Chris [mailto:[EMAIL PROTECTED]] Sent: Monday, March 05, 2001 2:33 PM To: 'Kenneth R Zink II'; Php (E-mail) Subject: RE: [PHP] Is it odd or even??? > That won't work. % returns the remainder from a division of > the number divided by the mod number. Yours: > if($num %2 == 0){ > echo "even"; > }else{ > echo "odd"; > } Mine: > > if( $num % 2 ) { > > echo "Odd"; > > > > } else { > > echo "Even"; > > > > } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Is it odd or even???
> Color me confused because I though true was any non zero > value and false was zero. Right. And if( $num % 2 ) { echo "it's odd"; } means that the operation returned a remainder - a non zero value. > I know I am using the following code: > if (!($num % 4){ do something} > and it does something when $num is evenly divisible by 4 (ie. > $num % 4 = 0). > If I were testing for odd vs even I'd do the following: > if (!($num % 2) { > Odd And this is: If( NOT ZERO ) { Odd } which is the same as: if( $num % 2 ) { If there is a remainder, it's not zero. Chris
RE: [PHP] Is it odd or even??? Optimize!!!
You all are a bunch of un-optimizing novices. Just do some bit banging. i.e. if(1 & number){ echo "$number is odd"; }else{ echo "$number is even"; } -Original Message- From: Brandon Orther [mailto:[EMAIL PROTECTED]] Sent: Monday, March 05, 2001 12:18 PM To: PHP User Group Subject: [PHP] Is it odd or even??? Hello, Is there an easy way to check and see if a number is odd or even? Thank you, Brandon Orther WebIntellects Design/Development Manager [EMAIL PROTECTED] 800-994-6364 www.webintellects.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Is it odd or even??? Optimize!!!
Clever. For those of us unfamiliar with bitwise ops, here's how this works: The bitwise and op (&) works on bits like this: dig1 dig2 Result 000 010 100 111 An even number's binary representation always ends with 0 (ie 12 = 1100) while an odd ends with 1 (ie 13 = 1101). So doing a bitwise & with 1 will only produce 1 if the last binary digit is 1 (ie an odd num), otherwise 0 (see table). And, like Nathan implies, this will be very fast (way faster than modulus), since you only ever deal with 2 bits of info. For an explanation of other bitwise operators, look here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html J on 3/5/01 12:36 PM, Nathan Cassano at [EMAIL PROTECTED] wrote: > > You all are a bunch of un-optimizing novices. Just do some bit banging. > > i.e. > > if(1 & number){ > echo "$number is odd"; > }else{ > echo "$number is even"; > } > > > -Original Message- > From: Brandon Orther [mailto:[EMAIL PROTECTED]] > Sent: Monday, March 05, 2001 12:18 PM > To: PHP User Group > Subject: [PHP] Is it odd or even??? > > > Hello, > > Is there an easy way to check and see if a number is odd or even? > > Thank you, > > > Brandon Orther > WebIntellects Design/Development Manager > [EMAIL PROTECTED] > 800-994-6364 > www.webintellects.com > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]