How can PHP be used to dynamically load content based on the selected language on a website?

To dynamically load content based on the selected language on a website, we can use PHP to detect the language selection from the user and then load the corresponding content from language-specific files or arrays. This can be achieved by setting a language variable based on user selection and then using this variable to include the appropriate language file or array containing the content.

// Get the selected language from the user
$selected_language = $_GET['lang'];

// Define language-specific content files
$language_files = [
    'en' => 'content_en.php',
    'fr' => 'content_fr.php',
    'es' => 'content_es.php'
];

// Include the appropriate language file based on the selected language
if(array_key_exists($selected_language, $language_files)) {
    include $language_files[$selected_language];
} else {
    // Default to English if selected language is not found
    include 'content_en.php';
}