[PHP]Check a number odd or even

Two small function in PHP, checking a number is odd or even.

With % :

function odd_or_even_v1($var) {
    if ($var % 2) {
         echo "$var is an ODD number.";
    } else {
         echo "$var is an EVEN number.";
    }
}

With & :

function odd_or_even_v2($var) {
    if ($var & 1) {
        echo "$var is an ODD number.";
    } else {
        echo "$var is an EVEN number.";
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s