PHP Constants
A constant is a name for a value that cannot change while the script runs. Constants are perfect for configuration values, limits and fixed labels: once set, nobody can accidentally overwrite them.
Constants differ from variables in a few ways:
| Variable | Constant | |
|---|---|---|
| Prefix | $name |
no $: NAME |
| Can change | yes | no |
| Scope | local to a function unless global | available everywhere, including inside functions |
| Naming convention | camelCase / snake_case |
UPPER_SNAKE_CASE |
Defining constants with const
The const keyword defines a constant at the top level of a file (or inside a class). The value must be known at compile time — a literal or an expression built from other constants.
<?php
const SITE_NAME = "TAD Market";
const MAX_ITEMS = 50;
const TAX_RATE = 0.08;
const GREETING = "Welcome to " . SITE_NAME;
echo GREETING, "\n";
echo "Cart limit: ", MAX_ITEMS, "\n";
echo "Tax on 100: ", 100 * TAX_RATE, "\n";Welcome to TAD Market
Cart limit: 50
Tax on 100: 8
Defining constants with define()
define() is a function, so it runs at run time. That means you can use it inside if blocks or build the name dynamically. defined() checks whether a constant exists, and constant() reads one whose name is stored in a string.
<?php
$env = "production";
if ($env === "production") {
define("DEBUG", false);
} else {
define("DEBUG", true);
}
var_dump(DEBUG);
var_dump(defined("DEBUG"));
var_dump(defined("UNKNOWN_SETTING"));
define("COLOR_PRIMARY", "#777bb4");
$which = "PRIMARY";
echo constant("COLOR_" . $which), "\n";bool(false)
bool(true)
bool(false)
#777bb4
Tip: Prefer
constfor ordinary constants — it is clearer and slightly faster. Reach fordefine()only when you need a condition or a computed name.
Constants are global
Unlike variables, constants can be used inside any function without global or parameters:
<?php
const CURRENCY = "USD";
const FREE_SHIPPING_FROM = 40;
function shippingMessage(float $total): string {
if ($total >= FREE_SHIPPING_FROM) {
return "Free shipping!";
}
$missing = FREE_SHIPPING_FROM - $total;
return "Add " . number_format($missing, 2) . " " . CURRENCY . " for free shipping.";
}
echo shippingMessage(25.5), "\n";
echo shippingMessage(60), "\n";Add 14.50 USD for free shipping.
Free shipping!
Array constants
Constants can hold arrays too, which is handy for fixed lists such as allowed values:
<?php
const SIZES = ["S", "M", "L", "XL"];
define("WEEKEND", ["Saturday", "Sunday"]);
echo SIZES[2], "\n";
echo count(SIZES), " sizes\n";
echo in_array("Sunday", WEEKEND) ? "Sunday is weekend\n" : "Workday\n";
echo implode("/", SIZES), "\n";L
4 sizes
Sunday is weekend
S/M/L/XL
Built-in constants
PHP predefines many constants that you can use straight away:
| Constant | Meaning |
|---|---|
PHP_EOL |
end-of-line character(s) for the platform |
PHP_INT_MAX / PHP_INT_MIN |
integer limits |
PHP_FLOAT_EPSILON |
smallest float difference |
M_PI, M_E |
π and e |
PHP_VERSION |
version of the running PHP |
E_ALL, E_WARNING |
error reporting levels |
Note: Trying to change a constant (
MAX_ITEMS = 60;) is a syntax error, and using an undefined constant throws anErrorin PHP 8. Constant names are case-sensitive.
Test yourself
How do you read a constant named LIMIT?
$LIMITLIMITconst(LIMIT)${LIMIT}
Which way of defining a constant can be used inside an if block?
constdefine()- Both work identically everywhere
- Neither
Can a function use a constant defined outside it without global?
- No, it needs
global - Yes, constants are available in every scope
- Only if passed as a parameter
- Only class constants