如何拆分if-else错误条件


How to split if else error condition

我用一些if/else语句进行了验证。

<?php
if (isset($_POST["join"])) { 
  if ($userpoint < $lessonpoint) { //pt
    echo "you need more points";
  } //pt
  else { //has enough point

      if ($row['num'] > 0) { //check if user took this lesson
        echo "you took this lesson before.";
      } //check if user took this lesson ends

    else { //then let him apply to database
            //define post:
            $postvalue = (int)$_POST["postvalue"];
            //and check
            if($postvalue == '' || $postvalue <= 0 || $postvalue > $minimumpostvalue || $postvalue == is_int($postvalue)) { //check post
            echo "Error."; 
            } //checkpost ends.

      else { //insert
      $sql = "INSERT into etc... VALUES (?, ?, ?)";
            if($sql){ //to another database
                $artibir = "UPDATE etc.";
                echo "Done.";
            } // to another database
      }//insert
    } //let him apply
  } //has enough point
} //if post isset join
?>

这个效果很好。

但我想针对这种情况回显另一条错误消息:$postvalue > $minimumpostvalue

在尝试时,我迷失在if/else语句中。无论我把新语句放在哪里,我都会出错。

所有变量都已定义。

在何处以及如何放置$postvalue > $minimumpostvalue以回显不同的错误消息?

<?php
if (isset($_POST["join"])) {
   if ($userpoint < $lessonpoint) { //pt
     echo "you need more points";
   } //pt
    else { //has enough point
         if ($row['num'] > 0) { //check if user took this lesson
             echo "you took this lesson before.";
         } //check if user took this lesson ends
        else { //then let him apply to database
             //define post:
             $postvalue = (int) $_POST["postvalue"];
             //and check
                  if ($postvalue == '' || $postvalue <= 0 || $postvalue > $minimumpostvalue || $postvalue == is_int($postvalue)) { //check post
                        if ($postvalue > $minimumpostvalue) {
                            echo "Another Error.";
                         } 
                        else {
                           echo "Error.";
                         }
                   } //checkpost ends.
                  else { //insert
                       $sql = "INSERT into etc... VALUES (?, ?, ?)";
                       if ($sql) { //to another database
                           $artibir = "UPDATE etc.";
                           echo "Done.";
                       } // to another database
                  } //insert
              } //let him apply
     } //has enough point
} //if post isset join
?>

这是另一个毫无例外的变体。

一旦$valid变为false,它将跳过下一次验证。

<?php
$valid = true;
$error = '';
if ($valid && !isset($_POST["join"])) {
    $error = 'Not a join post request';
    $valid = false;
}
if ($valid && ($userpoint < $lessonpoint)) {
    $error = 'You need more points';
    $valid = false;
}
...
if($valid) {
    // Database insert; redirect
} else {
    // User error feedback
}
//and check
if ($postvalue > $minimumpostvalue) { //check exception
   echo "Error 1."; 
} elseif ($postvalue == '' || $postvalue <= 0 || $postvalue == is_int($postvalue)) { //check the rest
      echo "Error 2."; 
} //checkpost ends.

这是未经测试的代码,更多的是如何避免嵌套if语句的示例。

关键是要发现尽早声明您处于错误状态的条件,并尽可能快地退出,最好是抛出异常并避免else语句。

为了简单起见,我只使用了'RunTimeException(),但我很可能会根据情况定义自己的异常。然后可以捕获异常,并根据其类型显示不同的错误页面。

/**
 * @param int $postvalue
 * @param int $minimumpostvalue
 */
function saveToDatabase($postvalue)
{
    if ($postvalue == '' || $postvalue <= 0 || $postvalue == is_int($postvalue)) {
        throw new 'RuntimeException('Error 2');
    }
    $sql = "INSERT into etc... VALUES (?, ?, ?)";
    if ($sql) {
        $artibir = "UPDATE etc.";
    }
}
if (!isset($_POST["join"])) {
    throw new 'RuntimeException('Not a join post request');
}
if ($userpoint < $lessonpoint) {
    throw new 'RuntimeException('You need more points');
}
$userHasTakenCourse = $row['num'] > 0;
if ($userHasTakenCourse) {
    throw new 'RuntimeException('User has already taken the course.');
}
$postvalue = (int) $_POST["postvalue"];
if ($postvalue > $minimumpostvalue) {
    throw new 'RuntimeException('Error 1');
}
saveToDatabase($postvalue);