defined
Checks whether a constant exists.
Syntax
defined(string $constant_name): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $constant_name | string | The constant name as a string. |
Return value
bool`true` if the constant is defined.
Examples
A default value for a constant:
<?php
if (!defined('ITEMS_PER_PAGE')) {
define('ITEMS_PER_PAGE', 20);
}
var_dump(defined('ITEMS_PER_PAGE'), defined('NO_SUCH_CONSTANT'));Output:
bool(true)
bool(false)Notes
Pass the name as a quoted string. Using a non-existent constant without the check raises an error.
See also
In the tutorial
- Constants — Constants in PHP — const and define, naming rules, built-in and magic constants.