If isset for constants, but not defined?

Cameleon picture Cameleon · Jun 22, 2011 · Viewed 17.5k times · Source

If I set a constant to = '',
How to I check if constant has something inside ?
(ie see if it is set to something other than the empty string.)

defined() does not do what I want, because it is already defined (as '').
isset() does not work with constants.

Is there any simple way ?

Answer

Linus Kleen picture Linus Kleen · Jun 22, 2011

The manual says, that isset() returns whether a "[...] variable is set and is not NULL".

Constants aren't variables, so you can't check them. You might try this, though:

define('FOO', 1);

if (defined('FOO') && 1 == FOO) {
// ....
}

So when your constant is defined as an empty string, you'll first have to check, if it's actually defined and then check for its value ('' == MY_CONSTANT).