PHP Constants

Constants are identifiers (names) for simple values that cannot change during script execution. They are useful for configuration values, limits, paths, and feature flags.

PropertyConstant
Declarationdefine("NAME", value) or const NAME = value;
AccessUse the name directly: NAME
ScopeGlobal (available everywhere after declaration)
MutabilityImmutable (cannot be changed once set)
TypesScalar, arrays (supported), resources not allowed

1) Using define()

define() declares a constant at runtime (inside conditionals/loops is allowed).

<?php
  define("SITE_NAME", "Codingwithsonu");
  define("ITEMS_PER_PAGE", 10);

  echo SITE_NAME;       // Codingwithsonu
  echo ITEMS_PER_PAGE;  // 10
?>
Note: Older PHP had a case_insensitive flag in define, but it’s deprecated/removed. Always use UPPER_SNAKE_CASE and case-sensitive names.

2) Using const

const is a language construct. It must be used at the top level (global scope) or inside classes, not inside if/loops.

<?php
  const APP_ENV = "development";
  const MAX_UPLOAD_MB = 20;

  echo APP_ENV;
?>
define vs const: Use const for compile-time constants (file/class scope). Use define() if you must declare conditionally or build the name dynamically.

3) Array Constants

Both define() and const support arrays as constant values.

<?php
  define("ROLES", ["admin", "editor", "viewer"]);
  const PAGINATION = [10,20,50];

  echo ROLES[0];      // admin
  echo PAGINATION[2]; // 50
?>

4) Class Constants

Use const inside classes. Access with self:: (from inside) or ClassName::CONST (from outside). They are always public in PHP.

<?php
  class Config {
    const DB_HOST = "localhost";
    const DB_NAME = "myapp_db";

    public static function dsn() {
      return "mysql:host=" . self::DB_HOST . ";dbname=" . self::DB_NAME;
    }
  }

  echo Config::DB_HOST; // localhost
  echo Config::dsn();
?>

5) Magic Constants

These are predefined and change depending on where they are used.

Magic ConstantMeaning
__LINE__Current line number of the file
__FILE__Full path and filename
__DIR__Directory of the file
__FUNCTION__Function name
__CLASS__Class name
__METHOD__Class method name
__TRAIT__Trait name
__NAMESPACE__Current namespace name
<?php
  echo __FILE__; // full path of this file
  echo __DIR__;  // directory path

  function demo() {
    echo __FUNCTION__;
  }
  demo();

  class C {
    public function m() {
      echo __CLASS__ . "::" . __METHOD__;
    }
  }
  $obj = new C();
  $obj->m();
?>

6) Namespaces & Constants

When using namespaces, constants live under that namespace unless declared as global.

<?php
  namespace App;

  const VERSION = "1.0.0";
  echo VERSION;          // 1.0.0 (inside App)
  echo \App\VERSION;  // global access with FQN
?>

7) Configuration Constants Example

Typical pattern for small apps (place in a separate config.php and include it):

<?php
  define("BASE_PATH", __DIR__);
  define("PUBLIC_PATH", BASE_PATH . "/public");
  define("UPLOAD_MAX_MB", 20);
  define("DEBUG_MODE", true);
?>

Best Practices

  • Use UPPER_SNAKE_CASE names: APP_ENV, API_URL.
  • Prefer const for file/class-level constants; use define() for conditional/runtime needs.
  • Keep configuration constants in a dedicated file (e.g., config.php).
  • For secrets, don’t hardcode constants — load from environment variables or a secure vault.
  • Use magic constants (__DIR__, __FILE__) for robust paths.

Practice Tasks

  1. Create constants for APP_NAME, APP_ENV, and DEBUG. Print different messages based on DEBUG.
  2. Make an array constant SUPPORTED_TYPES with ["jpg","png","gif"], check if a given extension is allowed.
  3. In a class MathUtil, define PI and create a static method to compute the area of a circle using self::PI.
  4. Print out __FILE__, __DIR__, and __LINE__ to understand their values.
Next: Move to Operators to perform calculations, comparisons, and logical operations using your variables and constants.