Language switch based on parameters and session state I use this to set language session for navigation. Someone said that this switch is too cumbersome. Why? Is there an easier way?

$defaultLang = 'it';

if (!empty($_GET["lang"])) {
    switch (strtolower($_GET["lang"])) {
        case "en":

            $_SESSION['lang'] = 'gb';
            break;
        case "tr":

            $_SESSION['lang'] = 'tr';
            break;
        default:

            $_SESSION['lang'] = $defaultLang;
            break;
    }
}

if (empty($_SESSION["lang"])) {
        $_SESSION["lang"] = $defaultLang;
}
Yes.  Definitely.  You can use a lookup array.

$langs = [
    'en' =&gt; 'gb',
    'tr' =&gt; 'tr',
];

$defaultLang = 'it';


if (isset($_GET['lang'])) {
    $lang = strtolower($_GET['lang']);
    $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
} elseif (!isset($_SESSION['lang'])) {  // meaning, no $_GET['lang'] and no $_SESSION['lang']
    $_SESSION['lang'] = $defaultLang;
}
// if there is no $_GET['lang'] and there is a $_SESSION['lang'], then nothing to update


A lookup array is concise and a breeze to maintain.  You only need to update the lookup or the fallback value; never the processing block.

?? is the null coalescing operator, so if the lang value is not found in the lookup, the fallback value will be used.

If your php version is not over 7 (then I urge you to upgrade) then you will need a longer condition syntax.

$_SESSION['lang'] = isset($langs[$lang]) ? $langs[$lang] : $defaultLang;