2010-05-23 75 views
8

我想在我的頁面上獲得一個變量以等於我擁有的交換機的結果。PHP獲取變量等於交換機的價值

這是代碼:

$payment_method = switch ($cardtype) { 
case "visa" : echo "VSA"; break; 
case "mastercard" : echo "MSC"; break; 
case "maestro" : echo "MAE"; break; 
case "amex" : echo "AMX" ; break; 
default : echo "Please specify a payment method!"; break; 
}; 

我怎樣才能獲得$ PAYMENT_METHOD等於這樣的結果????

到目前爲止,我收到了一個錯誤:

Parse error: syntax error, unexpected T_SWITCH in /var/www/account/credits/moneybookers/process.php on line 65 
+0

可能重複的[PHP麻煩與編碼變量(http://stackoverflow.com/questions/2875567/php-troubles-with-coding-variables) – 2010-05-23 14:09:14

+0

@col。彈片:大聲笑,同樣的問題來自同一個用戶% - ) – zerkms 2010-05-23 14:13:06

+0

不完全相同。他現在要求進行變量賦值,而不是回聲。但這就是爲什麼所有這些代碼都很糟糕的原因:他只是在沒有理解的情況下進行復制/拼湊。 – 2010-05-23 14:17:13

回答

5

你應該在的賦值開關:

switch ($cardtype) { 
    case "visa": 
     $payment_method = "VSA"; 
    break; 
    case "mastercard": 
     $payment_method = "MSC"; 
    break; 
    case "maestro": 
     $payment_method = "MAE"; 
    break; 
    case "amex": 
     $payment_method = "AMX"; 
    break; 
    default: 
     echo "Please specify a payment method!"; 
    break; 
}; 
2

您不能使用switch結構這種方式。您必須在case部件中指定$payment_method

在你的情況下,無論如何看你是echo,你可以刪除$payment_method =它應該工作。

是什麼非常非常容易,不過,是把所有的東西到一個數組:

$payment_methods = array(
    "visa" => "VSA", 
    "mastercard" => "MSC", 
    "maestro" => "MAE", 
    "amex" => "AMX" 
); 

if (!array_key_exists($cardtype, $payment_methods)) 
echo "Please specify a payment method!"; 
else 
echo "Your method: ".$payment_methods[$cardtype]; 
2

你應該這樣做:

$payment_method = ''; 

switch ($cardtype) { 
    case "visa" : $payment_method = "VSA"; break; 
    case "mastercard" : $payment_method = "MSC"; break; 
    case "maestro" : $payment_method = "MAE"; break; 
    case "amex" : $payment_method = "AMX" ; break; 
} 

if (strlen($payment_method)) 
{ 
    echo $payment_method; 
} 
else 
{ 
    echo "Please specify a payment method!"; 
} 
+0

默認值:$ payment_method =「請指定付款方式!」; //在此實現中,無法獲得付款方式是否有效。 – zerkms 2010-05-23 14:11:33

+1

@zerkms:對,固定主要,請考慮你的投票。謝謝 – 2010-05-23 14:12:26

+0

@Wed邏輯:固定;-) btw,當$ cardtype無效時,他將得到此代碼的通知 – zerkms 2010-05-23 14:14:56

8

這樣做:

$types = array('visa' => 'VSA', 'mastercard' => 'MSC', 'maestro' => 'MAE', 'amex' => 'AMX'); 
if (isset($types[$cardtype])) { 
    $payment_method = $types[$cardtype]; 
} else { 
    echo 'Please specify a payment method!'; 
} 
5

使用數組!

$types = array("visa"  => "VSA", 
       "mastercard" => "MSC", 
       "maestro" => "MAE", 
       "amex"  => "AMX"); 

$type = @$types[$cardtype] or echo "Please specify a payment method!"; 
2

爲了您應用,關聯數組將是最好的解決方案。要回答你的問題但是,你可以建立自己的開關/案例嗎?運營商

var card = 'amex'; //however you retrieve that value 

var method = card == 'visa' ? 'VSA' : card == 'mastercard' ? 'MSC' : card == 'maestro' ? 'MAE' : card == 'amex' : 'AMX' : null; //default value is null 

// store error msgs elsewhere in the code, to ease translation/alteration 
var errs = { 
'ENG', {'Please specify a payment method', '...', '...', '...'}, 
'ESP', {'Favor, indique la forma de pago', '...', '...', '...'}, 
'DEU', {'Bitte geben Sie eine Zahlungsmethode', '...','...','...'} 
} 

// alert the appropriate error message, in the current language 

var currentLang = 0; //however you set that value 
if(!method) alert(errs[currentLang][0]); 
+1

您不應該以這種方式使用三元運算符。可讀性是最重要的,在這種情況下,開關是最好的選擇。 – wlf 2012-12-05 11:23:19

1

如果你不想改變你的代碼(比如,如果你有很長的開關),您可以使用Output Buffering

<?php 
ob_start(); 
switch ($cardtype) { 
case "visa" : echo "VSA"; break; 
case "mastercard" : echo "MSC"; break; 
case "maestro" : echo "MAE"; break; 
case "amex" : echo "AMX" ; break; 
default : echo "Please specify a payment method!"; break; 
}; 
$payment_method = ob_get_clean(); 
?> 

ob_start()將開始輸出緩衝(這也將停止輸出)和ob_get_clean()將結束它,一切都恢復呼應,而OB運行。

因此,例如,如果$cardtype是簽證,$ payment_method將等於VSA。

希望它有幫助。的

Note: This might not be a good solution, if possible, please, use solution from any other answer. Pekka's answer is really good one, try his.