Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

define

Creates a constant at run time.

Syntax

define(string $constant_name, mixed $value): bool

Parameters

ParameterTypeDescription
$constant_namestringThe constant name.
$valuemixedThe value.

Return value

bool`true` on success.

Examples

A constant with a computed name:

<?php
 
define('APP_VERSION', '2.1.0');
 
foreach (['RED' => '#f00', 'GREEN' => '#0f0'] as $name => $value) {
    define('COLOR_' . $name, $value);
}
 
echo APP_VERSION, ' ', COLOR_GREEN;

Output:

2.1.0 #0f0

Notes

When the name is known in advance, write const APP_VERSION = '2.1.0';. define() is needed when the name or the value is computed.

See also

  • defined — Checks whether a constant exists.
  • constant — Returns the value of a constant by its name in a string.
  • getenv — Returns the value of an environment variable.

Related recipes

In the tutorial

  • Constants — Constants in PHP — const and define, naming rules, built-in and magic constants.

Thanks for reading!

Donate to the site — we work hard for you.