5
\$\begingroup\$

I have an API that returns the brand of a credit card. I want to make them look nice for my user (e.g. correct case and spacing). The possible options are amex, diners, discover, jcb, mastercard, unionpay, visa, or unknown. Here's how I am currently doing it:

if ($x == "jcb") {
    $x = strtoupper($x); 
} elseif ($x == "amex"){
    $x = "American Express";
} elseif ($x == "diners"){
    $x = "Diners Club";
} elseif ($x == "unionpay"){
    $x = "UnionPay";
} else {
    $x = ucfirst($x); 
}

Is there a more efficient way of doing this?

\$\endgroup\$
6

2 Answers 2

11
\$\begingroup\$

the code looks a bit messy. Personally I would make it either an array lookup,

$card_trans = [
    "jcb"      => "JCB",
    "amex"     => "American Express",
    "diners"   => "Diners Club",
    "unionpay" => "UnionPay",
];
$card = $card_trans[$card] ?? ucfirst($card);

or a match expression available with PHP 8+:

$card = match ($card) {
    "jcb"      => "JCB",
    "amex"     => "American Express",
    "diners"   => "Diners Club",
    "unionpay" => "UnionPay",
    default    => ucfirst($card),
};
\$\endgroup\$
5
  • \$\begingroup\$ For the line $card = $card_trans[$card] ?? ucfirst($card);, I would change it to $card = isset($card_trans[$card]) ? $card_trans[$card] : ucfirst($card); or $card = $card_trans[$card] ?: ucfirst($card);, depending of your tolerance for warnings, if it is required to support older versions of PHP. \$\endgroup\$ Nov 17, 2022 at 10:19
  • 2
    \$\begingroup\$ @IsmaelMiguel At some point, you have to assume that people will be running a vaguely recent version of PHP. ?? was added in PHP 7.0; that was released in 2015, and it's almost 4 years since either 7.0 or 5.6 received an official patch. If your PHP is even older, you need to use array( ... ) instead of [ ... ], but it's probably a waste of time to say that every time we use the "new" syntax. \$\endgroup\$
    – IMSoP
    Nov 17, 2022 at 17:15
  • 1
    \$\begingroup\$ @IMSoP That's a perfectly fine argument. Except, no PHP version was specified anywhere. Therefore, we can't truly assume anything. There are systems still running PHP 5.4 to this day, as some web hosting companies still give support to it. Also, PHP 5.4 introduced the square bracket syntax. But still, that's a nice catch. \$\endgroup\$ Nov 17, 2022 at 19:15
  • 1
    \$\begingroup\$ @IsmaelMiguel Indeed, but you have to draw the line somewhere - I wouldn't expect an answer to include caveats that objects in PHP 4 were pass-by-value, or that function definitions in PHP 2 (and 3?) used to have a syntax with no curly brackets. In my opinion, PHP 5.x is now old enough that anyone using it should be aware that most code samples they see won't just be copyable as-is. \$\endgroup\$
    – IMSoP
    Nov 17, 2022 at 21:00
  • \$\begingroup\$ @IMSoP I agree with you. I draw the line on "there are hosting servers that support 5.4" as just an afterthought. I wouldn't go any further down, as PHP 5.3 was when PHP started to be "modern". If written with care, any code written for PHP 5.4 can be used in PHP 8.1 without modifications. PHP 5.3 doesn't support self inside anonymous functions and the square brackets syntax, which will require to write the code a little too differently. Therefore, personally, I put the line in PHP 5.4. \$\endgroup\$ Nov 18, 2022 at 2:00
7
\$\begingroup\$

Using the array lookup, as YCS suggested, is a common convention to eliminate series of conditional statements. There are some other aspects of the code addressed below.

Use descriptive variable names

A variable name like $x for a string is not very descriptive of what the string value typically holds. A name like $card or $cardName makes it easier for anyone reading the code (including your future self) to know what each variable represents.

Consider using multiple variables

The variable $x is overwritten, though it may be useful to know the raw/original value later in the code. For this reason it may be wise to consider using a different variable for the updated name - e.g. $formattedCard.

Use strict type comparisons

The conditional statements use loose equality operators - i.e. ==. When the variable should usually be a string then strict equality comparison operators (e.g. ===) can be used. It is a good habit to use strict equality operators whenever possible.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.