1

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

2
  • You may want to read about Exceptions. Using die() is a relatively good solution when you must terminate the script execution. Commented Feb 28, 2011 at 13:42
  • @mailo exceptions shouldn't be used to handle application logic. Commented Feb 28, 2011 at 13:45

4 Answers 4

1

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");
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks... the only problem I have if the "return true" is any unknown value... like a customerID or first name" hard to pick what is an error message and what isnt
@Stevanicus : I updated but I'm not quite satisfied. It works, but looks ugly to me.
yea that what I put as my possible solution... yours looks cleaner than mine atleast :D but same idea. Like you im not quite satisfied either.
1

die terminates the execution of the PHP script at the line it is called. Therefore, your message would no be returned.

You might want to simply use return instead of die;

Comments

1

Yes, you could use die() to debug.

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

Yes, it returns the error message and yes, the script will stop continuing.

Comments

1

Well the only acceptable way in your case is return FALSE

if these functions are really methods of some class, use class variable to store actual errors. if these functions belongs to no class, I wouldn't use them at all, but write it in plain code

 if($user == "bob" && $pass = "password"){
      echo "Welcome bob";
 }else{
       echo "incorrect username or password");
 }

updated answer

Well there is nothing to invent. Just follow the way PHP goes:
Make your function return either value or FALSE

however, for the validation purpose you have to make it this way:

function validSomething($val){
  return (bool)rand(0,1);
}
$err = array();
if (!validSomething($var)) {
  $err[] = "Whatever error";
}

i.e. function returns only boolean values and actual error message being added by application logic.

However, in your example user-defined functions are totally misused.

9 Comments

the problem with just returning false is that, you wont know where the error is
@Stevanicus see updated answer. make your validation functions check one parameter at a time - so, you will always know. However, talking of such imaginary example a pain. I'd prefer real case to discuss.
$pass = "password" should be $pass == "password" here, typo ;) ?
Ouch typo by the OP, my bad :)
@Col. Shrapnel thanks for that... gave me an idea.. I could always return an array.. with the first "slot" being "success" or "error" for example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.