How can PHP be used to dynamically display content based on user-selected language options?
To dynamically display content based on user-selected language options in PHP, you can use an array to store translations for different languages and then retrieve the appropriate translation based on the user's selection.
<?php
// Define translations for different languages
$translations = [
'en' => [
'greeting' => 'Hello',
'message' => 'Welcome to our website!'
],
'fr' => [
'greeting' => 'Bonjour',
'message' => 'Bienvenue sur notre site web!'
]
];
// Get the selected language from user input
$selectedLanguage = $_GET['lang'] ?? 'en';
// Display content based on selected language
echo $translations[$selectedLanguage]['greeting'] . '<br>';
echo $translations[$selectedLanguage]['message'];
?>
Related Questions
- What are best practices for handling form submissions in PHP pop-up windows?
- What could be causing the "Undelivered Mail Returned to Sender" error when sending emails to a large number of users in PHP?
- How can PHP be utilized to connect server time with specific events or data updates on a website, such as changing DJ names at specific times?