【问题标题】:Comparison Operator "IF" in PHP [duplicate]PHP中的比较运算符“IF”
【发布时间】:2012-12-29 12:36:42
【问题描述】:

可能重复:
nested php ternary trouble: ternary output != if - else

为什么会这样:

if($db->f('item_bonus') > 0)
    $bonus = $db->f('item_bonus').' (i)';
elseif($db->f('manufacturer_bonus') > 0)
    $bonus = $db->f('manufacturer_bonus').' (m)';
elseif($db->f('category_bonus') > 0)
    $bonus = $db->f('category_bonus'). ' (c)';

但是这样不行:

$bonus = $db->f('item_bonus') > 0 ? $db->f('item_bonus').' (i)' : $db->f('manufacturer_bonus') > 0 ? $db->f('manufacturer_bonus').' (m)' : $db->f('category_bonus') > 0 ? $db->f('category_bonus'). ' (c)' : '0';

我做错了什么? $db->f 返回数字,浮点型。

【问题讨论】:

  • 我建议用括号将语句分组。
  • PHP 的?: 操作符是counterintuitive 当你链接它时。此外,第一个版本更具可读性。
  • 当你现在不明白的时候,你怎么能假设你或任何其他人会在以后(一周内)明白呢?只是不要以这种方式使用?:,否则你会在自己的脚下开枪。
  • @Luntegg 因为我说的是:你稍后会看到这段代码,你可能不会再理解它,直到你调试它。您正在生成代码,这很难维护。
  • @KingCrunch 我意识到......好吧,谢谢,我会记住的。只是想做一个紧凑的代码。

标签: php if-statement


【解决方案1】:

尝试分组:

$bonus = $db->f('item_bonus') > 0 ? $db->f('item_bonus').' (i)' : ($db->f('manufacturer_bonus') > 0 ? $db->f('manufacturer_bonus').' (m)' : ($db->f('category_bonus') > 0 ? $db->f('category_bonus'). ' (c)' : '0'));

【讨论】:

    【解决方案2】:

    最好将三元运算符与简单的 if/else 语句一起使用,以便在将来需要再次检查代码时理解它们。所以在这种情况下最好让你发布的第一段代码。

    【讨论】:

      【解决方案3】:

      如果您的理由是取消硬编码的 if 语句,您可以将函数和数据分开...

      // Important stuff
      // Change the order of bonus importance using this array
      $bonus_precedence = array("item", "manufacturer", "category");
      // Change the symbol for each item with this associative array
      $bonus_markers = array(
        "item" => "i",
        "manufacturer" => "m",
        "category" => "c"
      );
      
      // Magic beyond here
      foreach($bonus_precedence as $bonus_type) {
        // Get the next bonus type
        $bonus = $db->f("{$bonus_type}_bonus");
        // Was there a bonus
        if($bonus > 0) {
          // Output the bonus in full and break the loop
          $bonus_marker = $bonus_markers[$bonus_type];
          $bonus .= " ($bonus_marker)";
          break;
        } 
      }
      

      如果硬编码无关紧要,请坚持使用长 if 语句。您可以轻松评论它,易于阅读和评论:

      // Only one bonus type is allowed
      // Item bonuses are the most important if there is
      // an item bonus we ignore all other bonuses
      if($db->f("item_bonus") > 0) {
        $bonus = "{$db->f("item_bonus")} (i)";
      
      // Manufacturer bonuses are the next most important
      } elseif($db->f("manufacturer_bonus") > 0) {
        $bonus = "{$db->f("manufacturer_bonus")} (m)";
      
      // Category bonus is the fallback if there are no 
      // other bonuses
      } elseif($db->f("category_bonus") > 0) {
        $bonus = "{$db->f("category_bonus")} (c)";
      }
      

      否则至少尝试使其易于理解:

      // Only one bonus type is allowed
      $bonus = 
      
        // Item bonuses are the most important if there is
        // an item bonus we ignore all other bonuses
        ($db->f("item_bonus") > 0 ? "{$db->f("item_bonus")} (i)" : 
      
          // Manufacturer bonuses are the next most important
          ($db->f("manufacturer_bonus") > 0 ? "{$db->f("manufacturer_bonus")} (m)" : 
      
            // Category bonus is the fallback if there are no 
            // other bonuses
            ($db->f("category_bonus") > 0 ? "{$db->f("category_bonus")} (c)" : "0")
          )
        );
      

      【讨论】:

      • 谢谢,但我不认为一切都必须如此复杂:)
      猜你喜欢
      • 2022-01-09
      • 2012-05-30
      • 1970-01-01
      • 2013-09-16
      • 2011-03-07
      • 2011-02-24
      • 2012-10-05
      • 1970-01-01
      • 2017-01-03
      相关资源
      最近更新 更多