PHP Constants

Constants let you store a value that never changes throughout a script's execution, giving important settings a clear, reusable name.

What Is a Constant?

A constant is a named container whose value is fixed once it is set and cannot be reassigned later in the same run. Constants are ideal for values that should stay the same everywhere, such as a site name, a tax rate, or a configuration flag. Unlike variables they are not prefixed with a dollar sign, and by widely followed convention their names are written in uppercase so they stand out in the code.

  • A constant's value cannot change after definition, which prevents accidental overwrites.
  • Constants are global by nature and can be read from any scope without the global keyword.
  • Names are case sensitive and conventionally written in uppercase, for example MAX_USERS.
  • Constants can only hold scalar values or arrays, not objects or resources.

Defining Constants Two Ways

There are two ways to create a constant. The define() function works anywhere in your code and can even set the name dynamically, while the const keyword is evaluated at compile time and must appear in the top level of a script or inside a class or namespace. For most everyday settings const is preferred because it is faster and clearer, but define() remains useful when the name or value is decided at runtime.

define() and const

<?php
define("SITE_NAME", "Hyring Learn");
const TAX_RATE = 0.08;

echo SITE_NAME, "\n";        // Hyring Learn

$price = 50;
$total = $price + ($price * TAX_RATE);
echo $total, "\n";            // 54
?>
Note: Trying to redefine a constant that already exists raises a notice and the second definition is ignored. Use defined() to check first if you are not sure whether a name has been taken.

Constant Arrays and Class Constants

Modern PHP allows a constant to hold an array, which is convenient for grouping related fixed values like a list of allowed roles. Classes can also declare their own constants with const, and you access them through the class name using the double colon operator. Class constants keep configuration close to the code that uses it and can be marked with visibility such as public or private.

Array constant and class constant

<?php
const ROLES = ["admin", "editor", "viewer"];
echo ROLES[0], "\n"; // admin

class Circle {
    const PI = 3.14159;
    public function area($r) {
        return self::PI * $r * $r;
    }
}

$c = new Circle();
echo round($c->area(2), 2), "\n"; // 12.57
?>

Magic and Built-in Constants

PHP predefines a number of constants for you, and it also exposes so-called magic constants whose value changes depending on where they appear. These are useful for logging, debugging, and writing portable code that needs to know about its own location or the running environment.

ConstantTypeWhat it holds
PHP_VERSIONBuilt-inThe version string of the running PHP engine
PHP_INT_MAXBuilt-inThe largest supported integer value
PHP_EOLBuilt-inThe correct end-of-line character for the platform
__LINE__MagicThe current line number in the file
__FILE__MagicThe full path and name of the current file
__FUNCTION__MagicThe name of the current function
Note: Reach for constants whenever a literal value appears more than once or carries meaning beyond the number itself. A named constant like MAX_LOGIN_ATTEMPTS documents intent far better than a bare 5 scattered through your code.

Exercise: PHP Constants

What happens if you call define() again on an existing constant?