【问题标题】:How to split if else error conditionif else 错误条件如何拆分
【发布时间】:2015-12-17 23:06:04
【问题描述】:

我有一些 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 &gt; $minimumpostvalue

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

所有变量都已定义。

我可以在哪里以及如何放置 $postvalue &gt; $minimumpostvalue 来回显不同的错误消息?

【问题讨论】:

  • 您的代码可能运行良好,但编码不正确 - 就像一道菜味道很好,但它的食谱太糟糕了,原来的厨师无法理解,这就是正在发生的事情。尝试尝试如何更好地编写它。我建议从缩进开始。
  • 这已经是并且将成为维护的噩梦。您迷失在 if-else-statments 中的事实大喊:代码异味。将您的代码重构为函数顶部 15 行,尽可能避免使用 else 语句,并尽早返回。

标签: php if-statement


【解决方案1】:
<?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
?>

【讨论】:

  • if ($postvalue == '' || $postvalue == is_int($postvalue) || $postvalue &lt;= 0 || $postvalue &gt; $minimumpostvalue) 这在逻辑上是完美的:)
  • Parse error: syntax error, unexpected T_ELSE in C:\\www\x\test.php on line 246 第 246 行是:else {$sql 之前
  • 请检查您的代码中左大括号和右大括号的数量。这在第一印象中似乎是不正确的。
  • @AbhishekSingh 我有 9 个左大括号和右大括号。很奇怪。
【解决方案2】:

这是另一个没有例外的变体。

一旦 $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
}

【讨论】:

    【解决方案3】:
    //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.
    

    【讨论】:

    • Parse error: syntax error, unexpected T_ELSE in C:\\www\x\test.php on line 254 第 254 行是:else {$sql 之前
    • 我的代码段没问题。您可能在其他地方有一个无与伦比的大括号。
    • 这就是我坚持的地方。当代码像我的问题帖子一样编写时,它是有效的,但是每次正确的编辑都会抛出一个我无法弄清楚的错误。
    【解决方案4】:

    这是未经测试的代码,也是如何避免嵌套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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多