PHP Constants

A constant is a named value that is set once and cannot change during the execution of a script. Unlike variables, constants do not use the dollar sign prefix, and once defined, their value remains fixed for the entire duration of the script. Constants are useful for values that should never be modified — such as site configuration settings, tax rates, maximum file sizes, or fixed mathematical values.

Defining Constants with define()

The most common way to define a constant in PHP is using the define() function. It takes the constant name as the first argument and its value as the second.

<?php
  define("SITE_NAME", "LearnPHP");
  define("MAX_UPLOAD_SIZE", 5242880);   // 5 MB in bytes
  define("PI_VALUE", 3.14159);

  echo SITE_NAME;          // Outputs: LearnPHP
  echo MAX_UPLOAD_SIZE;    // Outputs: 5242880
  echo PI_VALUE;           // Outputs: 3.14159
?>

Notice that constants are used without a dollar sign ($). Attempting to assign a new value to a constant after it has been defined will trigger an error.

Constant Naming Rules

  • Constant names are case-sensitive by default.
  • By convention, constant names are written in ALL UPPERCASE with underscores separating words.
  • Names must begin with a letter or underscore — not a number.
  • No dollar sign is used when defining or using a constant.
<?php
  define("TAX_RATE", 0.08);
  define("APP_VERSION", "2.1.0");
  define("_INTERNAL_KEY", "abc123");   // Valid - starts with underscore

  echo TAX_RATE;      // Outputs: 0.08
  echo APP_VERSION;   // Outputs: 2.1.0
?>

Defining Constants with the const Keyword

The const keyword is an alternative way to define constants. It is commonly used inside class definitions and at the top level of a script.

<?php
  const GRAVITY = 9.8;
  const COMPANY = "OpenTech";

  echo GRAVITY;    // Outputs: 9.8
  echo COMPANY;    // Outputs: OpenTech
?>

Difference Between define() and const

Featuredefine()const
Used at global scopeYesYes
Used inside functionsYesNo
Used inside classesNoYes
Can use expressions as valueYesLimited (PHP 5.6+)

Constants vs Variables

<?php
  // Variable - can change
  $discount = 10;
  $discount = 20;   // Reassigned without error

  // Constant - cannot change
  define("SHIPPING_FEE", 5.99);
  // define("SHIPPING_FEE", 8.00);  // This would generate a notice and be ignored
?>

The key practical difference: use a constant when the value must stay the same throughout the script, and use a variable when the value might need to change.

Global Availability of Constants

Unlike variables, constants are automatically available anywhere in the script — inside functions, inside loops, or anywhere else — without needing the global keyword.

<?php
  define("CURRENCY", "USD");

  function showPrice($amount) {
    echo $amount . " " . CURRENCY;   // Constant accessible inside function
  }

  showPrice(29.99);   // Outputs: 29.99 USD
?>

PHP Magic Constants

PHP has a set of built-in constants called magic constants. Their values change depending on where they are used in the script. They are surrounded by double underscores on each side.

<?php
  echo __LINE__;     // Outputs the current line number
  echo __FILE__;     // Outputs the full path and filename
  echo __DIR__;      // Outputs the directory of the file
  echo __FUNCTION__; // Outputs the function name (inside a function)
  echo __CLASS__;    // Outputs the class name (inside a class)
?>

Practical Example Using __DIR__

<?php
  // Useful for including files relative to the current file
  require_once __DIR__ . "/config.php";
  require_once __DIR__ . "/helpers.php";
?>

This pattern prevents file path issues that arise when scripts are called from different directories.

Checking If a Constant Is Defined

The defined() function checks whether a constant has already been set. This is useful for preventing duplicate definitions.

<?php
  if (!defined("APP_MODE")) {
    define("APP_MODE", "production");
  }

  echo APP_MODE;   // Outputs: production
?>

This pattern is common in configuration files to ensure a constant is only defined once, even if the file is included multiple times.

Key Points

  • Constants hold a fixed value that cannot be changed after definition.
  • Use define("NAME", value) or const NAME = value; to create a constant.
  • Constants do not use the $ prefix.
  • Constant names are typically written in ALL_CAPS with underscores.
  • Constants are globally accessible — no global keyword is needed inside functions.
  • PHP magic constants (like __LINE__ and __DIR__) change value based on where they appear.
  • Use defined() to check if a constant already exists before defining it.

Leave a Comment

Your email address will not be published. Required fields are marked *