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.
| Property | Constant |
|---|---|
| Declaration | define("NAME", value) or const NAME = value; |
| Access | Use the name directly: NAME |
| Scope | Global (available everywhere after declaration) |
| Mutability | Immutable (cannot be changed once set) |
| Types | Scalar, 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
?>
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;
?>
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 Constant | Meaning |
|---|---|
__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
constfor file/class-level constants; usedefine()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
- Create constants for
APP_NAME,APP_ENV, andDEBUG. Print different messages based onDEBUG. - Make an array constant
SUPPORTED_TYPESwith["jpg","png","gif"], check if a given extension is allowed. - In a class
MathUtil, definePIand create a static method to compute the area of a circle usingself::PI. - Print out
__FILE__,__DIR__, and__LINE__to understand their values.