Question: My project can switch between languages. The items are stored in a database, and using $\_GET['lang'] in the database gives back the correct items. For now, only English and French are in use, so it works with this code :

```
if ($_GET['lang'] == 'fr' OR ($_GET['lang'] == 'en')) {
    $header = getTranslation('header', $_GET['lang']);
    $footer = getTranslation('footer', $_GET['lang']);
} else {
    header('Location: error.php');
}

```

What I'm looking for is some way to be prepared in case a language is added in the db. The code below approaches what I need (and obviously didn't work).

```
while ($translations = $languagesList->fetch()) {
    if ($_GET['lang'] == $translations['code']) {
        $header = getTranslation('header', $_GET['lang']);
        $footer = getTranslation('footer', $_GET['lang']);
    } else {
        header('Location: language.php');
    }
}

```

Is there any way to create a code that would generate multiple if conditions based on the number of languages in the db ?

Answer: 
You should move the else part outside of the loop, as otherwise you will always execute it at some point in the loop iterations. Only when you have iterated through *all* possibilities, and you still have no match, then you can be sure there to have to navigate to the language page:

```
$header = null;
while ($translations = $languagesList->fetch()) {
    if ($_GET['lang'] == $translations['code']) {
        $header = getTranslation('header', $_GET['lang']);
        $footer = getTranslation('footer', $_GET['lang']);
        break; // No need to waste time to look further.
    }
}
if ($header === null) {
    header('Location: language.php');
}

```

But it would be smarter to [prepare an SQL statement](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) that gets you the result for the *particular* language only (with `where code = ?` and bind `$_GET['lang']` to it). Then you don't have to iterate in PHP, which will in general be slower than what the database can provide. Instead you can just see whether you had 0 or 1 records back (Or use `select count(*)` and check whether that is 0 or not).
Easiest is to have a mapping in an array, check if your language is present there, and if so, do stuff. Something like this;

```
$languages = array('en', 'fr', 'de', '...');
$default_language = 'en';
$language = in_array($_GET['lang'], $languages) ? $_GET['lang'] : $default_language;

$header = getTranslation('header', $language);

```

As you can see I've set a default langauge which is used if the language in the query var cannot be found. How to create that array of languages is up to you (a simple query should suffice).