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


PHP how to set variable1 if variable 2 equals certain value

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

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

任何帮助将不胜感激!

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;
}
$subtype = htmlspecialchars($_POST['subtype']);
if      ($subtype == "12m") {$subprice = 273.78; }
elseif  ($subtype == "6m")  {$subprice = 152.10; }
elseif  ($subtype == "1m")  {$subprice = 30.42; }

使用 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;
}
$subtype = htmlspecialchars($_POST['subtype']);
if      ($subtype == "12m") {$subprice = 273.78}
elseif  ($subtype == "6m")  {$subprice = 152.10}
elseif  ($subtype == "1m")  {$subprice = 30.42}