user342391
user342391

Reputation: 7847

php troubles with coding variables

I am trying to echo certain values if the variable $cardtype ==

     $paymentmethod = if( $cardtype == 'visa' ) echo 'VSA'; 
elseif ( $cardtype == 'mastercard' ) echo 'MSC'; 
elseif ( $cardtype == 'mastercard' ) echo 'MSC'; 
elseif ( $cardtype == 'maestro' ) echo 'MAE'; 
elseif ( $cardtype== 'amex' ) echo 'AMX';

How would I do this???

Upvotes: 0

Views: 212

Answers (4)

Your Common Sense
Your Common Sense

Reputation: 157989

for your own code, you have to just remove strange $paymentmethod = from the beginning.

if( $cardtype == 'visa' ) echo 'VSA';  
elseif ( $cardtype == 'mastercard' ) echo 'MSC'; 
elseif ( $cardtype == 'maestro' ) echo 'MAE'; 
elseif ( $cardtype== 'amex' ) echo 'AMX';

it will work too.

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163318

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

echo ( isset( $types[ $cardtype ] ) ) ? $types[ $cardtype ] : 'Wrong card type';

Upvotes: 6

lamas
lamas

Reputation: 4598

You could use a function containing a switch statement for this:

function GetPaymentMethod( $cardtype )
{
    switch( $cardtype )
    {
    case 'visa':
      return 'VSA';
    case 'mastercard':
      return 'MSC';
    case 'maestro':
      return 'MAE';
    case 'amex':
      return 'AMX';
    default:
      return '<Invalid card type>';
    }
}

Test:

echo GetPaymentMethod( 'visa' ); // VSA

Upvotes: 2

baloo
baloo

Reputation: 7775

Here is one way to do it:

switch($cardtype) {
 case 'visa':
  echo 'VSA';
  break;
 case 'mastercard':
  echo 'MSC';
  break;
}

And so on

Upvotes: 0

Related Questions