constant

Returns the value of a constant whose name is given as a string at runtime.

Signature
constant(string $name): mixed
  • Added in PHP 4.0.4

Parameters

ParameterTypeDescription
$namestringThe name of the constant to read. Use Class::CONST for a class constant or Enum::Case for an enum case, and include the full namespace for namespaced constants.

Return value

mixedThe value of the constant. Class constants and enum cases are supported using the ClassName::CONSTANT syntax.

Errors

Throws an Error if the named constant, class constant or enum case is not defined.

Changelog

  • PHP 8.1.0Enum cases can be looked up, so constant('Suit::Hearts') returns the case object.
  • PHP 8.0.0An undefined constant now throws an Error. Before 8.0 the call raised E_WARNING and returned null.

constant() reads a constant whose name you only know at runtime. Writing MAX_USERS in source hard-codes the name; constant($name) lets the name arrive from a variable, a configuration array, or a string you assembled.

When to reach for it

  • Mapping configuration keys such as "LOG_LEVEL" onto constants defined in a bootstrap file.
  • Reading a class constant or enum case by name, e.g. constant('Status::Active').
  • Fetching a value from a constant whose name is built up, e.g. constant("HTTP_$code").

What trips people up

Since PHP 8.0 a missing constant is an Error, not a warning that returns null. Guard the call with defined() first, or wrap it in try/catch (Error $e).

Namespaces are part of the name. Inside namespace App; the call constant('MAX') looks for a global MAX; you must pass 'App\MAX' to reach the namespaced one. constant() is also for constants only — it will not read a variable, a static property, or an object property, and it has no access to constants that were never defined at the point of the call.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Resolving a driver class name stored as a string in config to its registered constant value.
  • Reading an enum case dynamically when the case name comes from a database column.
  • Looking up an HTTP status message constant built from the numeric status code.

Frequently asked questions

What is the difference between constant() and just writing the constant name?

Writing the name directly is resolved when the file is compiled, so the name must be literal. constant() resolves the name at runtime, which is the only way to use a name held in a variable.

Does constant() work with class constants?

Yes. Pass the fully qualified name in ClassName::CONSTANT form, for example constant('App\Config::VERSION'). Since PHP 8.1 the same syntax also returns enum cases.

What happens if the constant does not exist?

As of PHP 8.0 constant() throws an Error. In PHP 7 and earlier it emitted an E_WARNING and returned null, so older code that checked for null needs updating.

More examples

Reading an enum case by name

The output shows constant() returning the actual enum case object (identical to Suit::Spades), not a copy, so a strict === comparison succeeds and ->value can be read from it.

Example · php
<?php
enum Suit: string {
    case Hearts = 'H';
    case Spades = 'S';
}

$name = 'Spades';
$case = constant(Suit::class . '::' . $name);

var_dump($case === Suit::Spades);
echo $case->value, "\n";

Safe lookup with a fallback

The first call prints int(300) from the defined constant; the second prints int(60) because defined() returned false and constant() was never called, avoiding the PHP 8 Error.

Example · php
<?php
define('CACHE_TTL', 300);

function setting(string $name, mixed $fallback): mixed {
    return defined($name) ? constant($name) : $fallback;
}

var_dump(setting('CACHE_TTL', 60));
var_dump(setting('QUEUE_TTL', 60));

Discussion

  • Be the first to comment on this lesson.