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

  • SEARCH
  • Home
  • 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 4009920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:56:11+00:00 2026-05-20T08:56:11+00:00

I was looking to see what would be the best way to handle errors

  • 0

I was looking to see what would be the best way to handle errors from functions. Is the “DIE” method appropriate?

ie. php function calls another, for example:

function login(){
     $result = verifyDetails("bob", "password123");
     if($result == "accepted"){
          echo "Welcome bob";
     }else{
          echo "Error!! \n";
          echo $result;
     }
}

function verifyDetails($user, $pass){
     if(empty($user) || empty($pass)){
          die("cannot be empty");
     }

     if($user == "bob" && $pass == "password"){
          return "accepted";
     }else{
          die("username or password incorrect");
     }
}

does the “DIE” method return the message or does everything come to a standstill?

thanks in advance


UPDATE

what if the output is not known?

for example. in the above example I have placed “accepted” as the only correct answer.

What if the return is a name or id number.. then you cant really separate the error and correct returns.

thanks again.


UPDATE / Possible Solution

function login(){
     $result = verifyDetails("bob", "password123");
     if($result[0] == "SUCCESS"){
          echo "Welcome bob";
     }else if($result[0] == "ERROR"){
          echo "Error!! \n";
          echo $result;
     }else{
          echo "Unknown Error!!";
     }

}

function verifyDetails($user, $pass){
     $msg = array();

     if(empty($user) || empty($pass)){
          $msg[0] = "ERROR";
          $msg[1] = "cannot be empty"
          return $msg;
     }

     if($user == "bob" && $pass == "password"){
          //say $customerID is extracted from a db
          $msg[0] = "SUCCESS";
          $msg[1] = $customerID
          return $msg;
     }else{
          $msg[0] = "ERROR";
          $msg[1] = "username or password incorrect"
          return $msg;
     }
}

ideas & suggestions on the above “possible” solution are welcome


UPDATE

Check Shikiryu update 2 answer below for a cleaner version using arrays

  • 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-05-20T08:56:12+00:00Added an answer on May 20, 2026 at 8:56 am

    die() just echo your message and stop the script because die() is an alias of exit().

    In your case, since the password isn’t password but password123, the script will stop just displaying “username or password incorrect”.

    But as I can see here, you want a return "cannot be empty";, so that it’ll display :

    Error!!
    username or password incorrect

    (and optionally the rest of the html which die() won’t)


    Update 2 (ugly way) :

    function login(){
         $result = verifyDetails("bob", "password123");
         if(isset($result['success']){ // verifyDetails return true?
              echo $result['success'];
         }else{
              echo "Error!! \n";
              echo $result['error']; // Display the error verifyDetails throws
              // You may want to check if $result['error'] exists.
         }
    }
    
    function verifyDetails($user, $pass){
         if(empty($user) || empty($pass)){
              return array('error'=>"cannot be empty");
         }
    
         if($user == "bob" && $pass == "password"){
              return array('success'=>"Welcome bob");
         }else{
              return array('error'=>"username or password incorrect");
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to make somthing looking like this: The text
I am looking to see what is the best way to implement asp.net membership
Looking to see if there's an existing method or application to increase display size
I'm looking to see if I can design a HtmlHelper extension method that will
Basically just looking to see if you can capture an image from the webcam
I am looking for a way to see what the sql is that my
I'm looking for the best way to calculate a nodes balance in an AVL-tree
Two questions: If the user says 'no' what is the best way to handle
What is the best way to compare two strings to see how similar they
What would be the best way to send an alert message to the user

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.