define
Creates a constant at run time.
Syntax
define(string $constant_name, mixed $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $constant_name | string | The constant name. |
| $value | mixed | The 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 #0f0Notes
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
Related recipes
In the tutorial
- Constants — Constants in PHP — const and define, naming rules, built-in and magic constants.