This is a discussion on Detect Even Number within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Is there a function in PHP to detect even or odd numbers? I need a quick and easy way to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
huwpioden@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} |
|
|||
|
On 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 |
|
|||
|
On 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 > > 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} 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. |
|
|||
|
..oO(C.)
>On 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote: >> >> 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. 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 |
|
|||
|
C. wrote on 02/04/2008 01:56:
>> >> It is called AND (&) >> > 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. |
|
|||
|
Sylvain SF wrote:
> 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. I agree with "mod" being the best solution, at least if context isn't known. You *ARE* right with efficiency, but in most cases i'd trade that for code readability UNLESS execution time was CRITICAL. And if time WAS critical, I probably wouldn't be using PHP altogether for the task. i'd use if ($a % 2 === TRUE) >> This can also handle more generic cases (a multiple of N) > do you mean cases other than "odd or even" ? I think he does. > can you list these cases please ? Of course. Being "odd" and "even" synonyms for "not a multiple of two" and "a multiple of two" respectively, generic cases might be testing for $a being multiple of numbers other than two. IE: if ($a % $b === TRUE), where $b can be any integer. regards, f. |
|
|||
|
Cujo wrote on 17/04/2008 23:25:
> > generic cases might be testing for > $a being multiple of numbers other than two. certainly and these cases do not fall under the how to "detect an even number" question. thanks for these obvious and OOT comments. Sylvain. |
|
|||
|
..oO(Cujo)
>You *ARE* right with efficiency, but in most cases i'd trade that for >code readability UNLESS execution time was CRITICAL. And if time WAS >critical, I probably wouldn't be using PHP altogether for the task. > >i'd use > >if ($a % 2 === TRUE) This will always be FALSE, because the return type of the modulo operator is not a boolean. If you want such a complete expression, compare with an integer: if ($a % 2 === 0) { ... } Micha |
|
|||
|
Sylvain SF wrote:
> certainly and these cases do not fall under the how to "detect an even > number" question. Of course they don't, if they did we wouldn't be talking about handling **more generic** cases in the first place, would we ? > thanks for these obvious and OOT comments. You're welcome. regards, f. |