constant()
The constant() function in PHP is used to retrieve the value of a constant.
What constant() Does
The constant() function returns the value of a constant whose name is supplied as a string. This page covers its signature, why you would ever reach for it instead of writing the constant directly, how it works with class and enum constants, and the gotchas to watch for.
If you only know the name of a constant at runtime — for example because it was built from another variable — constant() is the way to look it up. To learn how constants are created in the first place, see PHP Constants and the define() function.
Syntax and Return Value
constant(string $name): mixed$name— the name of the constant as a string. For a class constant, use the fully qualifiedClassName::CONSTANTform.- Returns the value of the constant. The return type matches whatever the constant holds (
int,string,float,bool,array, or an enum case).
If the constant is not defined, constant() throws an Error (a \Error exception) in PHP 8 and later. Check first with defined() when you are not sure.
Basic Example
The constant PI is defined with the value 3.14159. constant("PI") retrieves that value, which is then used to calculate the area of a circle. The output is:
The area of a circle with radius 5 is 78.53975.When Would I Use It?
In this basic example constant("PI") does exactly the same thing as writing PI directly — so there is no reason to use it here. The function earns its place only when the constant's name is not known until runtime. Because you cannot write a variable where a constant name belongs, constant() lets you resolve a name held in a string:
<?php
const LEVEL_LOW = 1;
const LEVEL_HIGH = 9;
$severity = 'HIGH';
$value = constant("LEVEL_$severity");
echo "Severity level: $value";This prints Severity level: 9. The constant chosen depends on $severity, something a hard-coded LEVEL_HIGH reference could never do.
Class and Enum Constants
constant() also resolves class constants and enum cases when you pass the Class::NAME form as a string:
<?php
class Http
{
const NOT_FOUND = 404;
}
echo constant('Http::NOT_FOUND'); // 404For an enum, constant() returns the matching case object, so you can read its value:
<?php
enum Suit: string
{
case Hearts = 'H';
case Spades = 'S';
}
$case = constant('Suit::Hearts');
echo $case->value; // HGuarding Against Undefined Constants
Passing the name of a constant that does not exist throws an Error. Pair constant() with defined() to check first:
<?php
if (defined('TIMEOUT')) {
echo constant('TIMEOUT');
} else {
echo 'not defined';
}When TIMEOUT has not been defined, this prints not defined instead of crashing.
Gotchas
- It takes a string, not the constant itself. Writing
constant(PI)(no quotes) tries to use the value ofPIas the name, which is almost never what you want. - Constant names are case-sensitive.
constant('pi')will not findPI. - Undefined names throw. Guard with
defined()as shown above. - Use it only for dynamic names. If you know the constant name when you write the code, reference it directly — it is clearer and faster.