I am trying to get a variable on my page to equal the result of a switch I have.
This is the code:
$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;
};
How can I get $payment_method to equal the result of this????
So far I recieve an error:
Parse error: syntax error, unexpected T_SWITCH in /var/www/account/credits/mo开发者_如何学编程neybookers/process.php on line 65
do in this way:
$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!';
}
Use arrays!
$types = array("visa" => "VSA",
"mastercard" => "MSC",
"maestro" => "MAE",
"amex" => "AMX");
$type = @$types[$cardtype] or echo "Please specify a payment method!";
You should assign the value within the switch:
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;
};
You can't use the switch
construct this way. You would have to assign $payment_method
within the case
parts.
In your case, seeing as you're echo
ing anyway, you can just remove $payment_method =
and it should work.
What would be much, much easier, though, is putting all the stuff into an array:
$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];
For your application, an associative array will be the best solution. To answer your question however, you can build your own switch/case out of ? operators
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]);
You should do:
$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!";
}
If you don't want to change your code (like if you have very long switch), you can use 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()
will start output buffering (which will also stop output) and ob_get_clean()
will end it, returning everything echoed while ob running.
So, for example, if $cardtype
is visa, $payment_method will equal to VSA.
Hope it helps.
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.
Use match
statement.
https://www.php.net/manual/en/control-structures.match.php
Example:
$message = match ($statusCode) {
200, 300 => null,
400 => 'not found',
500 => 'server error',
default => 'unknown status code',
};
Also: https://stitcher.io/blog/php-8-match-or-switch
精彩评论