In what ways can PHP switch case statements be optimized for handling different national anthems in a login system?

When handling different national anthems in a login system using PHP switch case statements, it is important to optimize the code for efficiency and readability. One way to do this is by using an associative array to map each country to its respective national anthem, reducing the number of case statements needed and making the code easier to maintain.

// Define an associative array mapping countries to their national anthems
$nationalAnthems = [
    "USA" => "The Star-Spangled Banner",
    "UK" => "God Save the Queen",
    "France" => "La Marseillaise",
    // Add more countries and their national anthems as needed
];

// Get the selected country from the login form
$selectedCountry = $_POST['country'];

// Check if the selected country exists in the array and display the national anthem
if (array_key_exists($selectedCountry, $nationalAnthems)) {
    $nationalAnthem = $nationalAnthems[$selectedCountry];
    echo "The national anthem of $selectedCountry is: $nationalAnthem";
} else {
    echo "National anthem not found for $selectedCountry";
}