Code Comments
Programming Forum and web based access to our favorite programming groups.Is there a function in PHP to detect even or odd numbers? I need a quick and easy way to detect even numbers in a script I'm writing. Maybe phrased the search wrong but searching the manual doesn't come up with anything useful. TIA Huw
Post Follow-up to this messagehuwpioden@gmail.com schrieb:
> Is there a function in PHP to detect even or odd numbers? I need a
> quick and easy way to detect even numbers in a script I'm writing.
> Maybe phrased the search wrong but searching the manual doesn't come
> up with anything useful.
>
> TIA
>
> Huw
Yes :-) In every langauge.
It is called AND (&)
$a = 5;
if ($a&1){// is odd}
else
{// is even}
Post Follow-up to this messageOn Mar 31, 2:27 pm, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote:
>
> Yes :-) In every langauge.
> It is called AND (&)
>
> $a = 5;
> if ($a&1){// is odd}
> else
> {// is even}
Thanks Olaf. I can up with a different solution eventually - but sort
of based on the same idea. Thanks anyway.
Huw
Post Follow-up to this messageOn 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote:
> huwpio...@gmail.com schrieb:> Is there a function in PHP to detect even or
odd numbers? I need a
>
>
>
> Yes :-) In every langauge.
> It is called AND (&)
>
> $a = 5;
> if ($a&1){// is odd}
> else
> {// is even}
You should get out more Olaf - that won't work in most languages.
The mod operator is a far more sensible solution (portable across
strongly typed languages and applicable to different problems):
if ($val % 2) {
print "$val is odd\n";
} else {
print "$val is even\n";
}
This can also handle more generic cases (a multiple of N)
C.
Post Follow-up to this message..oO(C.) >On 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote: > >You should get out more Olaf - that won't work in most languages. Binary operations work in almost every language. The code just checks if the least significant bit in the number is set, which is the fastest way to do what the OP wants. >The mod operator is a far more sensible solution But inefficient, even if it doesn't really matter in this case. Micha
Post Follow-up to this messageC. wrote on 02/04/2008 01:56: > You should get out more Olaf hmm, have a nice day, too. > that won't work in most languages. The mod operator is a far more > sensible solution (portable across strongly typed languages and > applicable to different problems): you believe typed languages do not support binary operator ? you believe php is a typed language ? an odd number, for almost all CPUs, is a number whose lower bit is set to '1', full point, what could be the remaining part of a (very expensive) modulus operation is irrelevant. > if ($val % 2) which is not a portable across all typed languages expression! not all of them evaluate a numerical value as a boolean value. > This can also handle more generic cases (a multiple of N) do you mean cases other than "odd or even" ? can you list these cases please ? Sylvain.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.