defined()
defined() — Checks whether a constant with the given name exists.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
defined(string $constant_name): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $constant_name | string | The constant name. |
Return value
Returns true if the named constant given by constant_name has been defined, false otherwise.
Example
A runnable snippet:
/* Note the use of quotes, this is important. This example is checking * if the string 'TEST' is the name of a constant named TEST */ if (defined('TEST')) { echo TEST; } interface bar { const test = 'foobar!'; } class foo { const test = 'foobar!'; } var_dump(defined('bar::test')); // bool(true) var_dump(defined('foo::test')); // bool(true)
See also
define(), constant(), get_defined_constants(), function_exists()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page defined() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.