PHP how to find Odd and Even numbers

Here is how to find odd and even numbers in php.

How to find if a number is odd or even?
In PHP I learn to use the MOD (%) operator. see below for an example: $i = 10;

if ($i % 2) {
echo "$i is odd";
} else {
echo "$i is even";

You can also use the PHP “&” operator that will give you better performance
You can use the PHP code like so:

$i = 10;
if ( $i&1 )
echo "$i is odd";
echo "$i is even";

You may also like...

12 Responses

  1. sameer says:

    plz tell me how to validate number

  2. Off1 says:

    Thank you man, it's simplier than I tought!

  3. Tom Dings says:

    Great solution .. Way too easy 😉

  4. Nick says:

    Thanks a lot mate, very nice solution

  5. gumtree says:

    thanks mate! looking for this

  6. Exactly what i was looking for to sort out an absolutely massive list with each item duplicated. wrote a quick script, popped everything into an array and then just showed the even numbered entries. Half an hour of tedious deleting became five minutes of work. Thanks!! 🙂

  7. digital_artist says:

    nice. Thanks for sharing

  8. simon says:

    is_numeric is also a good way to validate a number 😉

  9. ah, is it that simple? Its been years since I write any code. Thanks for the effective tut!

  10. kwacky1 says:

    Thanks for that, so easy

    $week = date(‘W’, $timestamp ) & 1 ? ‘odd’ : ‘even’;

  11. katrina kaif says:

    Thats a great tips , i am thinking about this some days now i got the idea .thanks for share @admin

Leave a Reply