1

How to check if a value is even or odd?

while ($N=0 > 6; ++$N) { // might be mistake here but it just here for demonstration my cod differs
    ++$CELL;
    echo ($CELL/3)." - ";
    if(($CELL/3) % 2) {                        
        echo ($CELL/3)." (odd) <br>";
    } else {
        echo ($CELL/3)." (even) <br>";    
        echo "<tr>";
        };

   };

Can someone explain why it keeps outputing

0.333333333333 - 0.333333333333 (even)
0.666666666667 - 0.666666666667 (even)
1 - 1 (odd)
1.33333333333 - 1.33333333333 (odd)
1.66666666667 - 1.66666666667 (odd)
2 - 2 (even) 

should not it be

0.333333333333 - 0.333333333333 (even)
0.666666666667 - 0.666666666667 (even)
1 - 1 (odd)
1.33333333333 - 1.33333333333 (even)
1.66666666667 - 1.66666666667 (even)
2 - 2 (odd) 

kind of messed up even and odd but its not the point... is there better way to check if value is even?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Hallon
  • 11
  • 1
  • 1
  • 2
  • 1
    Are you trying to check if decimal numbers are odd or even? Looks like odd and even only apply to integers. Based on your example, you are more likely checking if it's float or integer. – Sufendy Apr 01 '11 at 08:24

7 Answers7

6

if your intent is for every third row to be "odd" as per your example, then you need:

if ($N % 3 == 2) {
   // odd
} else {
   // even
}
Alnitak
  • 334,560
  • 70
  • 407
  • 495
3

This is much easy with this code

 while ($N=0 $N > 6; $N++) {
        if ($N%2==0){
          echo 'Value is even';
       } else {
          echo 'value is odd';
       }

    }
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

"kind of messed up even and odd"

You, my friend, are the king of understatement.

If you want to check if a number has no fractional component then you need to convert it to an integer and compare it with the original.

$v = 3.4;
if ($v == (int)$v) {
  echo "Integer";
} else {
  echo "Fractional";
}
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I guess you want it to round up before you check if it's even or odd. use the function ceil to round it up.

if(ceil($CELL/3) % 2) {                        
        echo ($CELL/3)." (odd) <br>";
    } else {
        echo ($CELL/3)." (even) <br>";    
        echo "<tr>";
        };

but 2 is definitely an even number

Sufendy
  • 1,212
  • 2
  • 16
  • 29
1

Actually, it does not make sense to check if a fractional number is odd or even. A number is even when that number modulo 2 is 0. For instance, if you divide any fractional number by 2 it will always be odd, just because if you divide that number by 2 you do not have an exact result.

So, please let us know what are you tring to do here so we can help you better

ArtoAle
  • 2,939
  • 1
  • 25
  • 48
  • okay lets say i need to do certain action every 3rd step of a loop e.g. 3,6,9,12,... 900, 903 etc maybee i need to check if number is how to say it full not have any decimals, how can i do it. so i can devide by 3 and see if number is full its every third step then. – Hallon Apr 01 '11 at 18:24
  • Mmm, if you need yo do an action avery 3 steps you can do something like this: if( ($i % 3) == 0 ) and inside this test block do whatever you want :) – ArtoAle Apr 01 '11 at 23:01
0

use % (mod) operator.

for($N=0 $N > 6; $N++) { 
     ($N%2)? echo 'Value is even'; :  echo 'value is odd';
}
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
0

Operands of modulus are converted to integers (by stripping the decimal part) before processing.

http://www.php.net/manual/en/language.operators.arithmetic.php

josemimg
  • 241
  • 2
  • 4