14

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
3
  • possible duplicate of php troubles with coding variables Commented May 23, 2010 at 14:09
  • @Col. Shrapnel: lol, the same question from the same user %-) Commented May 23, 2010 at 14:13
  • Not quite the same. He's asking for the variable assignment now, not echo. But that's why all these codes a bad: he's just copy/baste it without understanding. Commented May 23, 2010 at 14:17

8 Answers 8

17

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!';
}
Sign up to request clarification or add additional context in comments.

Comments

12

Use arrays!

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

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

Comments

9

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

Comments

4

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

Comments

3

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]);

1 Comment

you shouldn't use the ternary operator in such a way. Readibility is paramount and in this case a switch is the best way to go.
3

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

Comments

2

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

5 Comments

default : $payment_method = "Please specify a payment method!"; // with this implementation it will be impossible to get is payment method valid or not.
@zerkms: right, fixed main, please consider your down vote. Thanks
@Wed Logic: fixed ;-) btw, he will get notice with this code, when $cardtype is invalid
@zerkms: What he would notice, could not get you?
now - nothing. i have commented the revision #2 without if(strlen()), which you have added later ;-)
1

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.

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.