Checking if a constant is empty

Just letting you know that you can do

if(!empty(MY_CONST))

since PHP 5.5.


if (!empty(constant('MY_CONST')) {
    ...
}

mixed constant ( string $name )

Return the value of the constant indicated by $name, or NULL if the constant is not defined

http://php.net/manual/en/function.constant.php


You can get along with this if for some reason you are still not using PHP 5.5.

if (defined('MY_CONST') && MY_CONST) {
    echo 'OK';
}

See the manual: empty() is a language construct, not a function.

empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

So you'll have to use a variable - empty() is really what you want in the first place? It would return true when the constant's value is "0" for example.

Maybe you need to test for the existence of the constant using defined() instead?

Tags:

Php