lundi 27 février 2017

Ternary IF-check not working properly

I'm trying to print out a specific image based on a user's cookie.

The cookie can have either of the 2 values: en or es. First I'd like to check if that cookie exists in the first place. If it does, then print out its value. Else, it should print out en.

It's a language cookie. en = english, es = spanish. And I have two flags, one english flag and one spanish flag, in /img/flags/ folder, and they have the names en.gif and es.gif

Here is my code:

<img src="/img/flags/<?php (isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en') ?>.gif">

Right now it just prints out an empty image (no file name) regardless if I have a cookie set or not: /img/flags/.gif

What am I doing wrong?

I'd rather use a ternary because it looks nicer. If I do a regular IF-check it works fine:

if (isset($_COOKIE['lang'])) {
  echo $_COOKIE['lang'];
} else {
  echo 'en';
}

Aucun commentaire:

Enregistrer un commentaire