even and odd

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattmcb
Forum Commoner
Posts: 27
Joined: Sun Jan 25, 2004 3:34 pm

even and odd

Post by mattmcb »

Is there a php function to tell if a number is even or odd?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<?php
if($num%2==0)
  echo "even";
else
   echo "odd";

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Or even [Edit: "even" as in "even this will work", not odd/even.... ah forget it]

Code: Select all

$even_or_odd = ($num%2 == 0) ? "even" : "odd";
echo $even_or_odd;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply