Constants
A constant is a name bound to a value that cannot change once it is defined. Where a variable like $price can be reassigned as many times as you like, a constant such as TAX_RATE is fixed for the entire lifetime of the script. Constants exist so that meaningful, fixed values — configuration flags, mathematical figures, API endpoints — have a readable name instead of a "magic number" or string scattered across your codebase.
Defining constants with define()
The original, function-based way to create a constant is define(). It takes the constant's name as a string and the value to bind to it, and it works at any point in a script — inside a function, inside a conditional, wherever you need it.
define() basics
<?php
define('SITE_NAME', 'Let Codes');
define('MAX_UPLOAD_MB', 25);
echo SITE_NAME;
echo "\n";
echo MAX_UPLOAD_MB;Let Codes 25
Notice that a constant is referenced without a leading $. That is one of the biggest differences from a variable, and it is the first thing that trips up newcomers: writing $SITE_NAME looks for an entirely different, undefined variable.
Defining constants with const
PHP 5.3 introduced a second, keyword-based syntax: const. It reads more like a variable declaration and is evaluated at compile time, which makes it the preferred style for constants whose value is known up front and does not depend on a runtime calculation.
const syntax
<?php const APP_VERSION = '3.2.0'; const PI_APPROX = 3.14159; echo APP_VERSION; echo "\n"; echo PI_APPROX;
3.2.0 3.14159
const also has a natural home inside classes, where it defines a class constant shared by every instance — for example a Circle class declaring const PI = 3.14159;, accessed as Circle::PI. That topic is covered in depth on the object-oriented programming pages; for now, just recognize that the same const keyword you use at the top level of a script is also how class-level constants are declared.
define() vs const: the practical differences
constis resolved at compile time, so it must be used at the top level of a file or inside a class — it cannot be placed inside anifblock or a function body.define()is a normal function call evaluated at runtime, so it can appear anywhere, including conditionally:if ($debug) { define('LOG_LEVEL', 'verbose'); }.constnames are always case-sensitive;define()accepted an optional third argument for case-insensitive constants in old PHP versions, but that option was removed as of PHP 8.0.constcannot hold the result of a function call or an expression involving variables, whiledefine()can compute a value at runtime, e.g.define('START_TIME', time());.
Naming convention: UPPER_SNAKE_CASE
By strong community convention, constant names are written in UPPER_SNAKE_CASE — all capital letters with underscores between words, such as MAX_RETRIES or DEFAULT_TIMEZONE. PHP does not enforce this; a constant named maxRetries works exactly the same way. Following the convention matters because it lets anyone reading your code instantly tell a constant apart from a function call or a class name, just by its casing.
Constants are immutable
Once a constant is defined, any attempt to define it again with the same name — whether through define() or const — fails. const redeclarations are caught at compile time as a fatal error; a repeated define() call at runtime simply emits a warning and leaves the original value untouched.
Attempting to redefine a constant
<?php
define('RETRIES', 3);
define('RETRIES', 5); // Warning: Constant RETRIES already defined
echo RETRIES;3
Checking whether a constant exists
Because referencing an undefined constant by name causes a fatal error in modern PHP (it used to only be a warning that printed the name as a string), it is good practice to guard with defined('NAME') before relying on a constant that might not have been set yet, for example when a config file is optional.
defined() as a safety check
<?php
if (!defined('APP_ENV')) {
define('APP_ENV', 'production');
}
echo APP_ENV;production
Built-in magic-free constants
PHP ships with a number of predefined constants that are always available. Two of the most commonly used are PHP_VERSION, a string such as "8.3.1" describing the running interpreter, and PHP_EOL, the correct end-of-line sequence for the current operating system ("\\n" on Linux/macOS, "\\r\\n" on Windows). Using PHP_EOL instead of hardcoding "\\n" keeps command-line scripts portable.
Predefined constants
<?php echo 'Running PHP ' . PHP_VERSION . PHP_EOL; echo 'Max int on this platform: ' . PHP_INT_MAX . PHP_EOL;
Running PHP 8.3.1 Max int on this platform: 9223372036854775807