constant()

constant() — Returns the value of a constant.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.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

constant(string $name): mixed

Parameters

ParameterTypeWhat it takes
$namestringThe constant name.

Return value

Returns the value of the constant.

Example

A runnable snippet:

define("MAXSIZE", 100); echo MAXSIZE; echo constant("MAXSIZE"); // same thing as the previous line interface bar { const test = 'foobar!'; } class foo { const test = 'foobar!'; } $const = 'test'; var_dump(constant('bar::'. $const)); // string(7) "foobar!" var_dump(constant('foo::'. $const)); // string(7) "foobar!"

Changelog

VersionChange
8.0.0If the constant is not defined, constant() now throws an Error exception; previously an E_WARNING was generated, and null was returned.

See also

define(), defined(), get_defined_constants()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page constant() (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.