PHP如果变量2等于某个值,如何设置variable1

时间:2012-05-17 12:11:54

标签: php arrays variables if-statement

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

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

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

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:5)

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;
}

答案 1 :(得分:2)

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

if      ($subtype == "12m") {$subprice = 273.78; }
elseif  ($subtype == "6m")  {$subprice = 152.10; }
elseif  ($subtype == "1m")  {$subprice = 30.42; }

答案 2 :(得分:1)

使用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;
}

答案 3 :(得分:0)

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

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