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/moneybookers/process.php on line 65
Each condition you want to match has a case where you pass in the variable you want to match. Within the case, you put the code you want to run if the condition matches. Then you need to add a break, otherwise the code will continue to check for matches in the rest of the switch statement.
Speed: A switch statement might prove to be faster than ifs provided number of cases are good.
The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. The switch-case statement tests a variable against a series of values until it finds a match, and then executes the block of code corresponding to that match.
A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Use arrays!
$types = array("visa" => "VSA",
"mastercard" => "MSC",
"maestro" => "MAE",
"amex" => "AMX");
$type = @$types[$cardtype] or echo "Please specify a payment method!";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With