0

Hi I'm trying to get one variable to set itself after an if statement on another variable, but I can't get the syntax right. Please help, this is the code that I've got so far.

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

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

Any help would be greatly appreciated!

4 Answers 4

5
if ($subtype == '12m')
  $subprice = 273.78;
elseif ($subtype == '6m')
  $subprice = 152.10;
elseif ($subtype == '1m')
  $subprice = 30.42;

Or with the switch statement:

switch ($subtype) {
  case '12m': $subprice = 273.78; break;
  case '6m' : $subprice = 152.10; break;
  case '1m' : $subprice = 30.42; break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@brew84 You're welcome. Please consider marking this as the accepted answer by clicking the checkmark outline to its left.
2
$subtype = htmlspecialchars($_POST['subtype']);

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

Comments

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.