paraphenylenediamine
paraphenylenediamine

Reputation: 23

PHP switch case with value from array

just started using php, sorry for the noob question. The values in the array are all strings of 3 letters for currencies and I want to output the "name" of the currency:

$c_title = $currency['title'];      
    foreach($c_title as $c_switch) {    
         switch($c_switch) {            
            case "AUD":             
                echo 'Australian Dollar';       
                break;              
            case "USD":                 
                echo 'US Dollar';       
                break;              
            case 'GBP':                 
                echo 'Pound Sterling';      
                break;                  
            case 'EUR':                 
                echo 'Euro';            
                break;              
            default:                
                echo $currency['title'];    
        }   
   }    
} 

Upvotes: 2

Views: 691

Answers (1)

mickmackusa
mickmackusa

Reputation: 47903

foreach($c_title as $c_switch) { indicates that $c_title (aka $currency['title'];) is an iterable variable (e.g. an array).

Then in the default: case, you write echo $currency['title']; which would only work if the data type was scalar / non-iterable (e.g. a string).

We don't know what data type you are using, but we can be sure that that is one problem that you must overcome. My suspicion is that you merely need to remove your foreach loop and just process the scalar value.

Beyond that, I prefer to avoid switch blocks because as your number of cases scales up, the length of your code bloats quickly.

I often recommend using a lookup array. This array may be a variable, but because the data is almost always static I like to show devs how to declare a constant.

Once the lookup is constructed, you only need to check if the input value is represented as a key in the lookup array before providing the translation. To succinctly execute this check, I prefer the null coalescing operator (??).

Code: (Demo)

define("CURRENCY_LOOKUP", [
    'AUD' => 'Australian Dollar',
    'USD' => 'US Dollar',
    'GBP' => 'Pound Sterling',
    'EUR' => 'Euro',
]);

$currency['title'] = 'JPY';
echo CURRENCY_LOOKUP[$currency['title']] ?? $currency['title'];

echo "\n---\n";

$currency['title'] = 'GBP';
echo CURRENCY_LOOKUP[$currency['title']] ?? $currency['title'];

Output:

JPY
---
Pound Sterling

Separating the translation data from the translation processing is going to keep your keep your code looking clean, readable, efficient, scalable, and professional into the future of your project. Whenever you wish to add more currency translations, you will only need to add new associative elements to the lookup -- you'll never need to touch the processing line. In other words, you add one line of code instead of three.


With PHP8, match() became an elegant hybrid of switch() and lookup techniques.

Code: (Demo)

$currency['title'] = 'GBP';

echo match($currency['title']) {
    'AUD' => 'Australian Dollar',
    'USD' => 'US Dollar',
    'GBP' => 'Pound Sterling',
    'EUR' => 'Euro',
    default => $currency['title']
};
// Pound Sterling

Upvotes: 3

Related Questions