2

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'];    
        }   
   }    
} 
4
  • What is your question? Instead of switch, I recommend a lookup array. Commented Nov 23, 2019 at 14:38
  • It doesn't work and I don't know why Commented Nov 23, 2019 at 14:38
  • var_dump($c_switch); And I suppose $c_title is a required string. Commented Nov 23, 2019 at 14:42
  • if it is possible please provide the input array and expected output. Commented Nov 23, 2019 at 19:05

1 Answer 1

3

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

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.