Introduction: Why PHP Constants Matter
In PHP, constants are identifiers for immutable values—once defined, they can’t be changed during script execution. This makes them ideal for storing values that should remain the same throughout your application, like API keys, database settings, or configuration flags.
By using constants effectively, you make your code more secure, readable, and maintainable. Let’s dive into how PHP constants work and how to use them the right way.
Table of Contents
What Is a PHP Constant?
A PHP constant is an identifier (name) for a value that does not change during the execution of the script. Constants:
-
Don’t use the
$
symbol. -
Are global and accessible anywhere.
-
Can only be defined once.
How to Define a Constant
You can define constants in two primary ways:
✅ Using define()
(Function-based)
define("SITE_NAME", "StudyZone4U");
-
Available since PHP 4
-
Value can be scalar or array (PHP 7+)
✅ Using const
(Keyword-based)
const MAX_USERS = 100;
-
Cannot be used in conditional blocks
-
Must be used at compile time
Using define()
vs const
Feature | define() |
const |
---|---|---|
Callable in blocks | ✅ Yes | ❌ No |
Scope | Global | Global (or class) |
Inside classes | ❌ No | ✅ Yes |
Expression support | Runtime only | Compile time only |
Version | PHP 4+ | PHP 5.3+ |
Tip: Use const
for class constants or when defining at the top level of a file.
Accessing Constants
Once defined, access constants without the $
prefix:
define("APP_VERSION", "1.0.0");
echo APP_VERSION; // Outputs: 1.0.0
You can also check if a constant is defined using defined()
:
if (defined("APP_VERSION")) {
echo "App version is " . APP_VERSION;
}
Constants and Scope
-
Constants are global by default.
-
Constants defined using
const
can be used in class contexts:
class Config {
const DB_HOST = "localhost";
}
echo Config::DB_HOST;
✨ Built-in Magic Constants
PHP comes with several predefined magic constants:
Magic Constant | Description |
---|---|
__LINE__ |
Current line number |
__FILE__ |
Full path and filename |
__DIR__ |
Directory of the file |
__FUNCTION__ |
Function name |
__CLASS__ |
Class name |
__METHOD__ |
Class method name |
__NAMESPACE__ |
Current namespace |
echo "This is line " . __LINE__; // Outputs: This is line X
✅ Complete Example
<?php
// Global constant
define("SITE_TITLE", "StudyZone4U");
// Constant using const
const MAX_LOGIN_ATTEMPTS = 5;
// Class constant
class Settings {
const SUPPORT_EMAIL = "[email protected]";
}
echo SITE_TITLE . "\n"; // StudyZone4U
echo MAX_LOGIN_ATTEMPTS . "\n"; // 5
echo Settings::SUPPORT_EMAIL . "\n"; // [email protected]
?>
⚠️ Tips & Common Pitfalls
✅ Tips
-
Use uppercase letters with underscores for readability:
API_KEY
,BASE_URL
-
Group constants in configuration files or classes
-
Use
const
for class-specific constants to enableClassName::CONSTANT
syntax
❌ Pitfalls
-
Don't reassign constants—you’ll get a fatal error:
define("VERSION", "1.0"); define("VERSION", "2.0"); // ❌ Error!
-
Avoid conditional
const
declarations—not allowed:if (true) { const SITE = "Blog"; // ❌ Error! }
-
Don’t prefix constants with
$
—they’re not variables.
Conclusion & Best Practices
Using constants in PHP is a smart way to centralize configuration and enforce immutability. They're ideal for values that should never change, like system settings, limits, or environment flags.
Best Practices
-
Define all app-wide constants in one config file.
-
Prefer
const
for class and compile-time constants. -
Use
define()
when dynamic declaration is necessary. -
Use meaningful, all-caps names with underscores.