Tomek Buszewski
Tomek Buszewski

Reputation: 7935

Create variable from constant

I want to ommit creating countless if/else statements and use predefined variables (or constants) instead.

DEFINE('LANG',$current_lang);

It returns either pl or en. And now I have two variables:

$pl = getid('o-nas');
$en = pll_get_post($pl, 'en');

How can I print the one I defined earlier with LANG? Or is there a better solution for that? Please note that I will have multiple cases of such things, but the LANG will always be either one of those two.

Upvotes: 0

Views: 33

Answers (1)

deceze
deceze

Reputation: 522005

The variable variable syntax for this case is:

define('FOO', 'bar');

$foo = 'baz';
$bar = 42;

echo ${FOO}; // 42

Note that this is not necessarily the best approach to i18n, but it answers your direct question.
A first step at improving this may simply be to use arrays for starters:

$content = [
    'en' => ..,
    'pl' => ..
];
echo $content[LANG];

The intent and syntax here is much more obvious and readable.

Upvotes: 2

Related Questions