tpae
tpae

Reputation: 6346

PHP Constants with variables

I know this is probably a newbie question, but is it possible to do this?

unserialize(LOG_ACTIONS_.''.strtoupper($language));

I have list of constants with _LANGUAGE which I want to use the variable $language with.

Example:

unserialize(LOG_ACTIONS_ENGLISH);

Here's the error I get:

Fatal error: Call to undefined function LOG_ACTIONS_strtoupper()

Upvotes: 0

Views: 123

Answers (2)

joschi
joschi

Reputation: 13101

You could probably just use the constant() function:

unserialize(constant('LOG_ACTIONS_'.strtoupper($language)));

Upvotes: 1

Pekka
Pekka

Reputation: 449803

Use constant() to get the constant's actual value.

unserialize(constant(LOG_ACTIONS_.''.strtoupper($language)));

I'm not sure whether having serialized data in a constant is really wise, though - it can be expensive if there's a lot of data in them.

I assume it's to circumvent the restriction that constants can't contain array values. This SO question shows some better ways to work around that:

What is the most "elegant" way to define a global constant array in PHP

Upvotes: 7

Related Questions