Language switch based on parameters and session statePHP: checking for $_GET variables with if...

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Why was the small council so happy for Tyrion to become the Master of Coin?

What is the meaning of "of trouble" in the following sentence?

Mean and Variance of Continuous Random Variable

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

Chess with symmetric move-square

What is the white spray-pattern residue inside these Falcon Heavy nozzles?

I see my dog run

least quadratic residue under GRH: an EXPLICIT bound

Simulate Bitwise Cyclic Tag

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Why is the design of haulage companies so “special”?

A Journey Through Space and Time

If Manufacturer spice model and Datasheet give different values which should I use?

Is it possible to make sharp wind that can cut stuff from afar?

Why Is Death Allowed In the Matrix?

Copycat chess is back

Schwarzchild Radius of the Universe

How to make payment on the internet without leaving a money trail?

Is there a minimum number of transactions in a block?



Language switch based on parameters and session state


PHP: checking for $_GET variables with if statementsCustom session handler class - is it robust enough?NHibernate session and transaction implementationFor a login portal, what security measures are needed to prevent unauthorized access?Authentication and session creationMultilanguage class that detects and sets languageImplementing internationalization functionality for online English lessonsUnique everlasting session with low collision rate and is set across sub-domainsSeemingly complicated way of displaying page viewsWrapper for Google_Service_Calendar as a Symfony service






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3












$begingroup$


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;
}









share|improve this question









New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
    $endgroup$
    – KIKO Software
    Apr 2 at 10:33




















3












$begingroup$


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;
}









share|improve this question









New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
    $endgroup$
    – KIKO Software
    Apr 2 at 10:33
















3












3








3





$begingroup$


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;
}









share|improve this question









New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




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;
}






php session i18n






share|improve this question









New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 2 at 19:56









200_success

131k17157422




131k17157422






New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 2 at 8:17









OgumOgum

182




182




New contributor




Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ogum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
    $endgroup$
    – KIKO Software
    Apr 2 at 10:33




















  • $begingroup$
    Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
    $endgroup$
    – KIKO Software
    Apr 2 at 10:33


















$begingroup$
Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
$endgroup$
– KIKO Software
Apr 2 at 10:33






$begingroup$
Please note that mickmackusa changed his answer, to make it do exactly the same as the code in your question, after you had accepted his answer as the best possible. You might need to correct your own code, to reflect this change, if you copied his code.
$endgroup$
– KIKO Software
Apr 2 at 10:33












1 Answer
1






active

oldest

votes


















4












$begingroup$

Yes. Definitely. You can use a lookup array.



$langs = [
'en' => 'gb',
'tr' => '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;





share|improve this answer











$endgroup$













  • $begingroup$
    I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
    $endgroup$
    – Ogum
    Apr 2 at 9:29










  • $begingroup$
    I've update my answer to provide a null coalescing operator replacement.
    $endgroup$
    – mickmackusa
    Apr 2 at 9:39










  • $begingroup$
    @Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
    $endgroup$
    – Script47
    Apr 2 at 10:48












Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Ogum is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216703%2flanguage-switch-based-on-parameters-and-session-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

Yes. Definitely. You can use a lookup array.



$langs = [
'en' => 'gb',
'tr' => '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;





share|improve this answer











$endgroup$













  • $begingroup$
    I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
    $endgroup$
    – Ogum
    Apr 2 at 9:29










  • $begingroup$
    I've update my answer to provide a null coalescing operator replacement.
    $endgroup$
    – mickmackusa
    Apr 2 at 9:39










  • $begingroup$
    @Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
    $endgroup$
    – Script47
    Apr 2 at 10:48
















4












$begingroup$

Yes. Definitely. You can use a lookup array.



$langs = [
'en' => 'gb',
'tr' => '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;





share|improve this answer











$endgroup$













  • $begingroup$
    I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
    $endgroup$
    – Ogum
    Apr 2 at 9:29










  • $begingroup$
    I've update my answer to provide a null coalescing operator replacement.
    $endgroup$
    – mickmackusa
    Apr 2 at 9:39










  • $begingroup$
    @Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
    $endgroup$
    – Script47
    Apr 2 at 10:48














4












4








4





$begingroup$

Yes. Definitely. You can use a lookup array.



$langs = [
'en' => 'gb',
'tr' => '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;





share|improve this answer











$endgroup$



Yes. Definitely. You can use a lookup array.



$langs = [
'en' => 'gb',
'tr' => '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;






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 2 at 15:31









Sᴀᴍ Onᴇᴌᴀ

10.2k62168




10.2k62168










answered Apr 2 at 9:03









mickmackusamickmackusa

2,079219




2,079219












  • $begingroup$
    I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
    $endgroup$
    – Ogum
    Apr 2 at 9:29










  • $begingroup$
    I've update my answer to provide a null coalescing operator replacement.
    $endgroup$
    – mickmackusa
    Apr 2 at 9:39










  • $begingroup$
    @Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
    $endgroup$
    – Script47
    Apr 2 at 10:48


















  • $begingroup$
    I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
    $endgroup$
    – Ogum
    Apr 2 at 9:29










  • $begingroup$
    I've update my answer to provide a null coalescing operator replacement.
    $endgroup$
    – mickmackusa
    Apr 2 at 9:39










  • $begingroup$
    @Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
    $endgroup$
    – Script47
    Apr 2 at 10:48
















$begingroup$
I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
$endgroup$
– Ogum
Apr 2 at 9:29




$begingroup$
I don't really know this type of operator: ??. i have a parse error unexpected '?' on $_SESSION['lang'] = $langs[$lang] ?? $defaultLang;
$endgroup$
– Ogum
Apr 2 at 9:29












$begingroup$
I've update my answer to provide a null coalescing operator replacement.
$endgroup$
– mickmackusa
Apr 2 at 9:39




$begingroup$
I've update my answer to provide a null coalescing operator replacement.
$endgroup$
– mickmackusa
Apr 2 at 9:39












$begingroup$
@Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
$endgroup$
– Script47
Apr 2 at 10:48




$begingroup$
@Ogum the alternative name for it is the 'isset ternary` because of what it does, it is essentially performing an isset check without the need for the true condition.
$endgroup$
– Script47
Apr 2 at 10:48










Ogum is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Ogum is a new contributor. Be nice, and check out our Code of Conduct.













Ogum is a new contributor. Be nice, and check out our Code of Conduct.












Ogum is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216703%2flanguage-switch-based-on-parameters-and-session-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...