doutao5499 2011-02-28 13:35
浏览 54
已采纳

PHP和函数错误

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!! 
";
          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!! 
";
          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

  • 写回答

4条回答 默认 最新

  • dounuo1881 2011-02-28 13:39
    关注

    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!! 
    ";
              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");
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示