0

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

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

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

任何帮助将不胜感激!

4

4 回答 4

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;
}
于 2012-05-17T12:14:39.773 回答
2
$subtype = htmlspecialchars($_POST['subtype']);

if      ($subtype == "12m") {$subprice = 273.78; }
elseif  ($subtype == "6m")  {$subprice = 152.10; }
elseif  ($subtype == "1m")  {$subprice = 30.42; }
于 2012-05-17T12:15:48.977 回答
1

Use PHP switch() to achieve that:

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

switch($subtype) {
  case "12m":
    $subprice = 273.78;
    break;
  case "6m":
    $subprice = 152.10;
    break;
  case "1m":
    $subprice = 30.42;
    break;
}
于 2012-05-17T12:17:55.840 回答
0
$subtype = htmlspecialchars($_POST['subtype']);

if      ($subtype == "12m") {$subprice = 273.78}
elseif  ($subtype == "6m")  {$subprice = 152.10}
elseif  ($subtype == "1m")  {$subprice = 30.42}
于 2012-05-17T12:16:27.937 回答