How can arrays be utilized effectively for multilingual content in PHP applications?
To handle multilingual content in PHP applications, arrays can be utilized effectively by storing language-specific strings as key-value pairs. By organizing translations in arrays, it becomes easier to manage and switch between different languages within the application.
// Define an array for English translations
$translations_en = array(
"welcome" => "Welcome to our website",
"contact" => "Contact us for more information"
);
// Define an array for French translations
$translations_fr = array(
"welcome" => "Bienvenue sur notre site web",
"contact" => "Contactez-nous pour plus d'informations"
);
// Set the desired language
$language = "en"; // or "fr"
// Retrieve a specific translation based on the selected language
echo $language === "en" ? $translations_en["welcome"] : $translations_fr["welcome"];