PHP defined() Function
The PHP defined() function is used to check whether the given constant exists and is defined.
Note: To check if a variable exists, use isset() as defined() only applies to constants. To check if a function exists, use function_exists().
Syntax
defined(constant_name)
Parameters
constant_name |
Required. Specify the name of the constant. |
Return Value
Returns true if the named constant given by constant_name has been defined, false otherwise.
Example: checking constants
The example below shows the usage of defined() function.
<?php //defining few constants const x = 100; define("Greeting", "Hello world!"); class foo { const test = "Hello"; } //checking if a given constant exists var_dump(defined("x")); var_dump(defined("Greeting")); var_dump(defined("foo::test")); echo "\n"; var_dump(defined("y")); var_dump(defined("GREETING")); var_dump(defined("foo::test1")); ?>
The output of the above code will be:
bool(true) bool(true) bool(true) bool(false) bool(false) bool(false)
❮ PHP Miscellaneous Reference