2012-05-17 50 views
0

您好我想獲得一個變量來設置自己後,另一個變量if語句,但我無法獲得正確的語法。請幫助,這是迄今爲止我已經得到的代碼。PHP如何設置變量1,如果變量2等於某個值

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

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

任何幫助將不勝感激!

回答

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

感謝完美的作品的幫助! – brew84

+0

@ brew84不客氣。請考慮將其標記爲接受的答案,方法是單擊左側的勾號輪廓。 – Indrek

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

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

if  ($subtype == "12m") {$subprice = 273.78} 
elseif ($subtype == "6m") {$subprice = 152.10} 
elseif ($subtype == "1m") {$subprice = 30.42} 
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; 
} 
相關問題