Constants
A constant is a name for a value that never changes during a script — a tax rate, a maximum upload size, a version number. Constants have no dollar sign, can't be reassigned, and are available everywhere. PHP also predefines hundreds of constants, including "magic" ones that describe where the code is.
Defining constants with const
Use the const keyword at the top level of a file (or inside a class — lesson 36). By convention, constant names are in UPPER_SNAKE_CASE:
<?php const VAT_RATE = 0.2; const APP_NAME = "Planner"; const WEEKDAYS = ["Mon", "Tue", "Wed", "Thu", "Fri"]; $net = 50; echo APP_NAME, ": gross price ", $net * (1 + VAT_RATE), "\n"; echo count(WEEKDAYS), " working days, starting ", WEEKDAYS[0], "\n";
Planner: gross price 60 5 working days, starting Mon
Constants can hold scalars, arrays, enum cases and (since PHP 8.1) objects created with new, and their value can be an expression built from other constants: const SECONDS_PER_DAY = 24 * 60 * 60;.
Constants can't change
<?php const MAX_USERS = 100; MAX_USERS = 200;
Parse error: syntax error, unexpected token "=" in main.php on line 4
Defining the same constant twice also fails, with a warning, and the original value is kept.
define()
The older define() function creates a constant at run time. Unlike const, it can be called inside an if and the name can be computed. defined() checks whether a constant exists, and constant() reads one by name:
<?php if (!defined('DEBUG')) { define('DEBUG', false); } $level = 'LOG_LEVEL_' . 'INFO'; define($level, 2); var_dump(DEBUG); echo constant('LOG_LEVEL_INFO'), "\n";
bool(false) 2
Prefer const for fixed values; it's checked at compile time and reads more clearly.
Predefined constants
PHP provides constants describing the environment and limits:
<?php echo PHP_MAJOR_VERSION, ".", PHP_MINOR_VERSION, "\n"; echo PHP_INT_MAX, "\n"; echo PHP_FLOAT_EPSILON, "\n"; echo PHP_OS_FAMILY === 'Windows' ? "Windows\n" : "Unix-like\n"; echo "Line one", PHP_EOL, "Line two", PHP_EOL; echo M_PI, "\n";
8.5 9223372036854775807 2.2204460492503E-16 Unix-like Line one Line two 3.1415926535898
PHP_EOL is the operating system's line ending — handy when writing text files. Many extensions add their own constants, such as JSON_PRETTY_PRINT, E_ALL or SORT_NUMERIC, which you pass as options to functions.
Magic constants
Magic constants start and end with two underscores, and their value depends on where they appear:
<?php function whereAmI(): void { echo "Function: ", __FUNCTION__, ", line ", __LINE__, "\n"; } whereAmI(); echo "File: ", basename(__FILE__), "\n"; echo "Line: ", __LINE__, "\n"; echo "Directory exists: ", is_dir(__DIR__) ? "yes" : "no", "\n";
Function: whereAmI, line 5 File: main.php Line: 10 Directory exists: yes
| Constant | Value |
|---|---|
__LINE__ | Current line number |
__FILE__ | Full path of the current file |
__DIR__ | Directory of the current file |
__FUNCTION__, __METHOD__ | Current function / class method name |
__CLASS__, __NAMESPACE__ | Current class / namespace |
__DIR__ is especially useful for including files relative to the current one: require __DIR__ . '/config.php'; works no matter which directory the script was started from.
Constants or enums?
A group of related constants — order statuses, user roles, sizes — is better expressed as an enum (lesson 40), which the type system can check. Use plain constants for single configuration values.
Try it yourself
Define constants for a shop's free-shipping threshold and shipping fee, and use them to price a few orders.
Order 12.50 + shipping 4.99 = 17.49 Order 49.99 + shipping 4.99 = 54.98 Order 50.00 + shipping 0.00 = 50.00 Order 120.00 + shipping 0.00 = 120.00