defined() In Action

<?php

const A = 1;

class C {
    public const D = 1;
    private const E = 1;
}

use const A as B;

// global constants
var_dump(defined('A'));
var_dump(defined('\A'));
var_dump(defined('\\A')); // good joke

var_dump(defined('B'));   // false
var_dump(defined(A));     // checking the constant value

// class constants
var_dump(defined('C::D'));
var_dump(defined('C::E'));  // including visibility

The defined() function checks if a constant is defined or not.

It is possible to check the constant with its name, and with its namespace.

Beware of quotes: most often, when checking for the constant’s value, the result is false.

On the other hand, it is not possible to check a constant with its aliased name: extra names, created with use are not validated.

To check for a class constant, it is possible to use a string, with the class name and the :: separator. Then, the visibility of the class constant is taken into account: it might be defined, but not accessible from that part of the code.

See Also

PHP Features

Last updated: 14 July 2026