开发者

PHP and Function Errors

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 bel开发者_如何学Pythonow for a cleaner version using arrays


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");
     }
}


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;


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜