I'm trying to write some PHP code to return a translation based on two parameters: an integer translation id and a two-letters language code.
For this, I nested switch statements. The outer switch works fine, but the inner switch fails and their default is always returned, whichever the value that the $lang parameter takes. In the example below, "Berne" is returned when $lang="de".
I checked, that the value of $lang is correct; it was. Casted $lang as (string) to be sure that it is of the correct type. Tried enclosing the inner switches between brackets, as well as putting double quotes instead of simple ones. Without success. Here's my code:
<?php
switch($id) {
case 3:
{
switch((string)$lang) {
case 'de':
$v = 'Bern';
break;
case 'en':
$v = 'Berne';
break;
default:
$v = 'Berne';
}
}
break;
case 4:
{
switch($lang) {
case 'de':
$v = 'Zürich';
break;
case 'en':
$v = 'Zurich';
break;
default:
$v = 'Zurich';
}
}
break;
default:
{
switch($lang) {
case 'de':
$v = 'Genf';
break;
case 'en':
$v = 'Geneva';
break;
default:
$v = 'Genève';
}
}
break;
}
return $v;
?>
var_dump($lang);var_dump($lang=='de');
after both$v = 'Berne';
and check again.echo mb_detect_encoding($lang)
to make sure it is in the correct encoding and you might want totrim($lang)
too.