Function return error/sucess

Discussion in 'PHP' started by encom, Oct 18, 2009.

  1. #1
    Hi,
    This is an extremely simplified version of my code:

    
    function action($id)
    {
         if($id == 6)
        {
              // Sucess
        }
        else
        {
             // Error
        }
    }
    
    $id = 6
    
    if(action($id) is error)
    {
         // Do something
    }
    else
    {
         // Do something
    }
    
    PHP:
    could someone complete that code for me. I hoppe it makes sence (If not please say :) )
     
    encom, Oct 18, 2009 IP
  2. Brandon_R

    Brandon_R Peon

    Messages:
    330
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    function action($id)
    {
         if($id == 6)
        {
              return true;
        }
        else
        {
             return false;
        }
    }
    
    $id = 6;
    
    if(action($id))
    {
         echo 'Success';
    }
    else
    {
         echo 'Failure';
    }
    PHP:
    The above will echo Success if your ID is 6 or Failure if the id is not 6.
     
    Brandon_R, Oct 18, 2009 IP
  3. encom

    encom Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    ahh thank you thats perfect.
     
    encom, Oct 18, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Better still just use
    
    function action($id) {
        return ($id == 6);
    }
    PHP:
    for the function instead
     
    JAY6390, Oct 18, 2009 IP