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???
$types = array( 'visa' => 'VSA', 'mastercard' => 'MSC',
'maestro' => 'MAE', 'amex' => 'AMX' );
echo ( isset( $types[ $cardtype ] ) ) ? $types[ $cardtype ] : 'Wrong card type';
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
Here is one way to do it:
switch($cardtype) {
case 'visa':
echo 'VSA';
break;
case 'mastercard':
echo 'MSC';
break;
}
And so on
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.
精彩评论