Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP const and define()

Reference for PHP const and define(): declare constants at compile time or run time, naming rules, array constants, namespaces and class constants.

A constant is a named value that cannot change once it is set. PHP offers two ways to create global constants: the const keyword and the define() function. Both create a name you use without a $, and both are visible in every scope.

The difference is when they run. const is a language construct that must appear at the top level of a file or namespace (not inside if, loops or functions), and its value is fixed when the file is compiled. define() is an ordinary function call, so it can run conditionally and take a computed name.

PHP
const MAX_USERS = 100;
define('APP_ENV', 'production');
echo MAX_USERS, " ", APP_ENV;

Try it

PHP const and define side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Redefinition, undefined constants and class constants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
const NAME = expression;
const A = 1, B = 2;
define(string $constant_name, mixed $value, bool $case_insensitive = false): bool
Name Description
NAME / $constant_name The constant's name. Letters, digits and underscores, not starting with a digit. By convention UPPER_SNAKE_CASE.
expression / $value A scalar, null, array or enum case. const accepts only constant expressions (literals, other constants, operators); define() accepts any runtime value, including objects since PHP 8.1.
$case_insensitive Obsolete. Case-insensitive constants are no longer supported; passing true only raises a warning. Leave it out.

Return value (define): true on success, false if the constant already exists (with a warning).

Errors: reading an undefined constant throws Error ("Undefined constant"), a change from the warning and string fallback of PHP 7.

Usage notes

  • const for fixed names, define() for dynamic ones. Use const by default; it is faster, works with namespaces and is easier to read. Reach for define() when the name or the condition is only known at run time.
  • Namespaces. const inside namespace App; creates App\NAME, while define('NAME', ...) creates a global unless you include the namespace in the string: define('App\NAME', 1).
  • Constants never change. Redefining returns false with a warning, and you cannot unset() one.
  • Arrays. Array constants are allowed and read-only: LIMITS['max'] works, but LIMITS['max'] = 5 is a compile error.
  • Check before reading. defined('NAME') tests existence; constant('NAME') reads a name held in a string and also works for Class::NAME.
  • Class constants are declared with const inside a class and read with Class::NAME or self::NAME. They can have visibility, final, and (PHP 8.3) a type; see Class constants.
  • Not case-insensitive. true, false and null are the only special-cased words; all your own constants are case-sensitive.
  • Magic and predefined constants such as __LINE__ and PHP_VERSION are listed in Magic constants and Predefined constants.
  • Configuration. For settings that differ between environments, prefer reading environment variables over constants hardcoded in code.