defined
Checks whether a constant, class constant or enum case with the given name exists.
defined(string $constant_name): bool- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$constant_name | string | The constant name to look for, quoted as a string. Use Class::CONSTANT for class constants, Enum::Case for enum cases, and include the namespace for namespaced constants. |
Return value
booltrue if the named constant is defined, false otherwise.
Errors
Does not throw. An unknown name simply returns false.
Changelog
- PHP 8.1.0Enum cases can be checked, so defined('Suit::Hearts') returns true.
defined() answers one question: does a constant with this exact name exist right now? It never triggers an error, which makes it the safe way to probe for a constant before reading it.
When to reach for it
- The
defined('X') || define('X', $default)idiom, used heavily by plugins and libraries that must not clobber a host application's settings. - Guarding
constant(), which throws anErrorfor unknown names since PHP 8.0. - Feature-detecting extension constants, e.g.
defined('CURLOPT_HTTP_VERSION_2TLS').
What trips people up
The name must be a string. Writing defined(MY_CONST) without quotes tries to evaluate the constant first, which is exactly the error you were trying to avoid. Namespaces matter too: from inside namespace App;, defined('MAX') checks the global MAX, so pass 'App\MAX' for the namespaced one.
Finally, defined() is not isset(). It does not see variables, and it returns true for a constant whose value is null or an empty string — existence and truthiness are separate questions.
Example
When to use it
- Letting a library ship defaults that a host application can override by defining the constant first.
- Checking that a required environment constant was set by the bootstrap before continuing.
- Detecting whether an optional extension is loaded by testing for one of its constants.
Frequently asked questions
What is the difference between defined and isset?
defined() checks constants; isset() checks variables and array keys. They look at completely different namespaces, so isset('FOO') will never find a constant.
Does defined work with class constants?
Yes, pass the name as 'ClassName::CONSTANT'. Since PHP 8.1 the same form also works for enum cases, such as defined('Suit::Hearts').
Why does defined return false for a constant I defined in a namespace?
define() creates global constants unless the namespace is written into the name string. Check with the fully qualified name, for example defined('App\\MAX').
More examples
The define-if-missing idiom
UPLOAD_LIMIT stays 1024 because it already existed and define() was short-circuited away; CACHE_TTL prints 600 because it was missing and got the default.
<?php
define('UPLOAD_LIMIT', 1024);
function ensure(string $name, mixed $value): void {
defined($name) || define($name, $value);
}
ensure('UPLOAD_LIMIT', 4096);
ensure('CACHE_TTL', 600);
echo UPLOAD_LIMIT, "\n";
echo CACHE_TTL, "\n";Existence is not truthiness
defined() returns true for both constants even though their values are false and an empty string, showing that the check reports existence only.
<?php
define('FEATURE_X', false);
define('LABEL', '');
var_dump(defined('FEATURE_X'));
var_dump(FEATURE_X);
var_dump(defined('LABEL'), LABEL === '');
Discussion