【问题标题】:PHP how to set variable1 if variable 2 equals certain value如果变量2等于某个值,PHP如何设置变量1
【发布时间】:2012-05-25 00:26:30
【问题描述】:

您好,我正在尝试让一个变量在对另一个变量执行 if 语句之后进行设置,但我无法正确使用语法。请帮忙,这是我目前得到的代码。

$subtype = htmlspecialchars($_POST['subtype']);

if      $subtype == ['12m'] {$subprice = 273.78}
elseif  $subtype == ['6m']  {$subprice = 152.10}
elseif  $subtype == ('1m')  {$subprice = 30.42}

任何帮助将不胜感激!

【问题讨论】:

    标签: php arrays variables if-statement


    【解决方案1】:
    if ($subtype == '12m')
      $subprice = 273.78;
    elseif ($subtype == '6m')
      $subprice = 152.10;
    elseif ($subtype == '1m')
      $subprice = 30.42;
    

    或使用switch 声明:

    switch ($subtype) {
      case '12m': $subprice = 273.78; break;
      case '6m' : $subprice = 152.10; break;
      case '1m' : $subprice = 30.42; break;
    }
    

    【讨论】:

    • @brew84 不客气。请考虑通过单击其左侧的复选标记大纲将此标记为已接受的答案。
    【解决方案2】:
    $subtype = htmlspecialchars($_POST['subtype']);
    
    if      ($subtype == "12m") {$subprice = 273.78; }
    elseif  ($subtype == "6m")  {$subprice = 152.10; }
    elseif  ($subtype == "1m")  {$subprice = 30.42; }
    

    【讨论】:

      【解决方案3】:

      使用 PHP switch() 来实现:

      $subtype = htmlspecialchars($_POST['subtype']);
      
      switch($subtype) {
        case "12m":
          $subprice = 273.78;
          break;
        case "6m":
          $subprice = 152.10;
          break;
        case "1m":
          $subprice = 30.42;
          break;
      }
      

      【讨论】:

        【解决方案4】:
        $subtype = htmlspecialchars($_POST['subtype']);
        
        if      ($subtype == "12m") {$subprice = 273.78}
        elseif  ($subtype == "6m")  {$subprice = 152.10}
        elseif  ($subtype == "1m")  {$subprice = 30.42}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-07-04
          • 2013-04-28
          • 1970-01-01
          • 2011-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-09
          相关资源
          最近更新 更多