Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2004
    Location
    Sydney Australia
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    detect an odd or even number function

    Is there a php function to detect an odd or an even number?
    wind is your friend

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is now (see below). Just use the modulus. Like
    PHP Code:
    function is_even($a){
        
    $even True;
        if (
    $a != '0'){
            
    $even False;
        }
        return 
    $even;
    }
    // example
    for ($i=0;$i<=1000;$i ++){
          if (
    is_even($i){
               echo 
    $i ' is an even number.<br />';
          } else {
               echo 
    $i ' is an odd number.<br />';
          }

    Last edited by raf; 04-15-2004 at 08:20 AM. Reason: added a break behind each ec (in case someone would run the code)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •