Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:07:13+00:00 2026-06-14T00:07:13+00:00

I need an advice how to handle function with lots of different return’s better.

  • 0

I need an advice how to handle function with lots of different return’s better.
I have this simple log in function in my class:

public function login($email, $password){

$record = // Go to database and find what we need.

if($record){
   // Check pass, $user_match = TRUE;
} else {
   return 1;         //No such user. Please register!
}

$active = (1 == $record['is_active']) ? TRUE : FALSE;
$verified = (1 == $record['is_verified']) ? TRUE : FALSE;

//User email and password matched:
if($user_match == true){
   if($verified === true){
       // Start session and insert to db table "online_users"
   // Check that user was inserted to table: $confirm = $stmt->rowCount();

         if($confirm !== 1){
           return 2; //Unexpected technical error. Please try again in a moment.
         }

       return 0;     //0 means that all clear, we good to go.

       } else {
           return 3; //not verified
       }
    } else {
        return 4;    // no match with email and pass, reject!
    }
}

The problem is, that now all the time I need to check all return’s, something like that:

$log = $user->login($_POST['email'], $_POST['pass']);
if($log === 0) {
    //Log in & edirect
} elseif ($log === 1) {
    //No such user. Tell to register
} elseif($log === 2){
    //technical error, try again
} elseif($log === 3){
    //Not verified
} elseif($log === 4){
    //wrong password

It’s really annoying right now, and imagine if I would need to check like 20 return’s? Is there any better way? How to do it more efficient and faster?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:07:14+00:00Added an answer on June 14, 2026 at 12:07 am

    You should rethink the purpose of this function and structure the return types accordingly. If the purpose is to log the user in, there can only be one answer: it either worked or it didn’t. So the primary return type of the function should be a boolean. How to differentiate between different causes for failure is a different topic.

    Option 1: throw exceptions:

    try {
        if (!$foo->login()) {
            echo 'Invalid credentials';
        }
    } catch (UserNotActiveException $e) {
        ...
    } catch (UserNotValidatedException $e) {
        ...
    }
    

    Not necessarily the most elegant option, since a failed login isn’t really an exceptional circumstance.

    Option 2: save the error state in the login provider:

    if (!$foo->login()) {
        echo "You're not logged in because ", $foo->loginError();
    }
    

    Whether this is good or not depends on how the class is used otherwise, you may not want to make it too stateful.

    Option 3: separate the problem of login from what is the user’s state entirely:

    if (!$foo->login($user)) {
        switch ($foo->currentStatus($user)) {
            case $foo::ALL_OK :
                 echo 'Login credentials invalid';
                 break;
            case $foo::ACCOUNT_INACTIVE :
                 ...
        }
    }
    

    Option 4: return a status object:

    $result = $foo->login();
    switch ($result->status) {
        case $result::ALL_OK :
            ...
    }
    

    That’s essentially what you’re doing now, just without the magic numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have this working but I need advice. I'd like to know
I need some advice in tackling a query. I can handle this in a
I need some expect advice on how to handle the following:- I have a
I need advice on using stored procedures with Entity Framwork 4.x to return data
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
I really need advice on how to do the following. I have tried several
I have 3 tables, I need advice on how to get data from them.
I need some advice. I have a web page and want to extend it's
Sorry if this is quite a long question, i need some advice on what
I need your advice\help here. I developed a universal App and plan to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.