defined()
The defined() function in PHP is used to check if a constant is defined or not.
What the defined() Function Does
defined() is a built-in PHP function that checks whether a constant with a given name already exists. It returns true if the constant is defined and false if it is not. This page covers its syntax, how to test class and namespaced constants, the common pitfall of confusing it with isset(), and when you would actually reach for it.
A constant is a name whose value cannot change during a single request — created with define() or the const keyword. Because referencing an undefined constant raises an error (an Error exception in PHP 8+, a notice in older versions), defined() lets you check first and avoid that.
Syntax
defined(string $constant_name): bool$constant_name— a string holding the name of the constant to look for. Note that you pass the name as a string ("GREETING"), not the constant itself (GREETING).- Returns
trueif a constant by that name exists,falseotherwise.
Passing the bare constant instead of its name —
defined(GREETING)— tries to evaluateGREETINGfirst, which fails if it is not defined. Always quote the name.
Basic Example
Because GREETING was created on the line above, defined("GREETING") returns true and the script prints Hello, world!.
Checking Class and Namespaced Constants
defined() also works with class constants and namespaced constants. Use the fully qualified name as a string:
<?php
const PI = 3.14;
class Circle {
const SHAPE = "round";
}
var_dump(defined("PI")); // bool(true)
var_dump(defined("Circle::SHAPE")); // bool(true) — class constant
var_dump(defined("Circle::MISSING"));// bool(false) — no such constant
var_dump(defined("UNDEFINED")); // bool(false)For class constants, the syntax is "ClassName::CONSTANT_NAME". For a namespaced constant you provide its full path, e.g. defined("App\\Config\\VERSION").
defined() vs. isset(): a Common Mix-up
defined() checks constants; isset() checks variables (and array keys / object properties). They are not interchangeable:
<?php
$name = "Alex"; // a variable
define("APP_NAME", "W3"); // a constant
var_dump(isset($name)); // bool(true) — variable exists
var_dump(defined("name")); // bool(false) — no constant called "name"
var_dump(defined("APP_NAME")); // bool(true) — constant existsIf you find yourself reaching for defined("$variable"), you almost certainly want isset() instead.
When to Use It
- Guarding optional config constants. Frameworks often let you predefine constants like
DEBUGorAPP_ENV; usedefined()to apply a default only when the user has not set one. - Preventing duplicate definitions.
define()re-defining an existing constant emits a warning, so wrap it:if (!defined("API_KEY")) { define("API_KEY", "..."); }. - Feature detection. Libraries expose marker constants (e.g.
PHP_VERSION_ID);defined()confirms a constant-based feature is available before relying on it.
To read a constant's value once you know it exists, pair defined() with constant(). For the bigger picture on creating constants, see PHP Constants and Class Constants.