Constants
Learn how to define constants in PHP using define() and const, and explore PHP's built-in magic constants.
Introduction
Some values in a program should never change once set — a tax rate defined by law, the maximum number of login attempts allowed, or the version number of your application. PHP's constants exist exactly for values like these.
In this lesson, you will learn the two ways to define a constant in PHP — the classic define() function and the more modern const keyword — along with PHP's built-in "magic constants" and a quick preview of constants inside classes.
- How to define a constant with define().
- How to define a constant with the const keyword.
- The UPPER_SNAKE_CASE naming convention for constants.
- What magic constants like PHP_VERSION and __LINE__ are.
- A brief look ahead at constants defined inside classes.
Defining Constants with define()
The classic way to create a constant is the built-in define() function, which takes the constant's name as a string and its value. Notice that constants are never written with a $ prefix.
<?php
define("SITE_NAME", "PrograMinds");
define("MAX_LOGIN_ATTEMPTS", 5);
echo SITE_NAME . "\n";
echo MAX_LOGIN_ATTEMPTS;
?>PrograMinds
5By strong convention, constant names are written in UPPER_SNAKE_CASE. This makes them instantly recognizable in code and visually distinct from ordinary $variables.
Once defined, a constant cannot be changed or removed — attempting to redefine it will cause an error (or in older PHP versions, a warning, and the second definition is simply ignored).
<?php
define("MAX_USERS", 100);
echo MAX_USERS . "\n";
// define("MAX_USERS", 200); // Fatal error: Cannot redefine an existing constant
?>100The const Keyword
Modern PHP code more commonly uses the const keyword to define constants. It reads more like a normal variable assignment and is evaluated at compile time, which makes it slightly faster and is required when defining constants inside classes.
<?php
const APP_NAME = "PrograMinds";
const TAX_RATE = 0.18;
echo APP_NAME . "\n";
echo TAX_RATE;
?>PrograMinds
0.18define()
- A regular function call
- Can be used inside conditional logic
- Evaluated at runtime
- Cannot be used inside a class body
const
- A language keyword
- Must be used at the top level or inside a class
- Evaluated at compile time
- The modern, recommended default
For most everyday scripts, prefer const — it is the modern standard. Reach for define() only in the rare case where you need to define a constant conditionally, since const cannot appear inside an if statement.
Magic Constants
PHP ships with several built-in constants, some of which are called "magic constants" because their value changes depending on where in the code they are used.
PHP_VERSION
A regular built-in constant holding the current PHP version as a string, e.g. "8.3.0".
__LINE__
The current line number in the file — its value changes depending on where it appears.
__FILE__
The full path and filename of the file currently being executed.
__DIR__
The full path of the directory containing the current file (equivalent to dirname(__FILE__)).
<?php
echo "PHP Version: " . PHP_VERSION . "\n";
echo "This is line: " . __LINE__ . "\n";
echo "File: " . __FILE__ . "\n";
echo "Directory: " . __DIR__;
?>PHP Version: 8.3.0
This is line: 3
File: /var/www/html/magic-constants.php
Directory: /var/www/html__FILE__ and __DIR__ are especially handy for building reliable file paths (e.g. for include or require) that work no matter where a script is called from, without hardcoding an absolute path.
Constants Inside Classes
Constants can also be defined directly inside a class using const, where they represent a value shared by every instance of that class. You have not covered classes yet, but here is a quick preview of what that looks like.
<?php
class Circle {
const PI = 3.14159;
}
echo Circle::PI;
?>3.14159Class constants are accessed using the :: (scope resolution) operator, as in Circle::PI. You will explore classes, objects, and this syntax in much more depth once you reach the Object-Oriented Programming lessons.
Common Mistakes
- Accidentally prefixing a constant name with $, as if it were a variable.
- Trying to redefine an existing constant, which triggers an error.
- Using const inside an if statement or loop — it is not allowed outside of the top level or a class body (use define() there instead).
- Confusing __FILE__ (the current file's path) with __DIR__ (that file's containing directory).
Best Practices
- Use UPPER_SNAKE_CASE for all constant names.
- Prefer const over define() in modern code unless you specifically need conditional definition.
- Use __DIR__ instead of hardcoded paths when including other files.
- Group related constants together near the top of a file or inside a dedicated configuration file.
Frequently Asked Questions
Do constants use a $ prefix like variables?
No. Constants are referenced by name alone, without a $ prefix, e.g. MAX_USERS rather than $MAX_USERS.
Can a constant's value change after it is defined?
No. Once a constant is defined, its value cannot be changed or removed for the rest of the script's execution.
What is the difference between define() and const?
define() is a function call evaluated at runtime and can be used conditionally; const is a language construct evaluated at compile time and is required for defining constants inside classes.
What does __LINE__ actually return?
It returns the line number, within the current file, where __LINE__ itself appears — useful for debugging and logging.
Key Takeaways
- Constants hold values that never change once defined, and never use a $ prefix.
- define() creates a constant at runtime; const is the modern, compile-time alternative.
- Constant names conventionally use UPPER_SNAKE_CASE.
- PHP provides magic constants like PHP_VERSION, __LINE__, __FILE__, and __DIR__.
- Classes can define their own constants using const, accessed via ClassName::CONSTANT.
Summary
Constants give you a way to store values that should never change during a script's execution. You can define them with define() or the more modern const keyword, and PHP also provides several useful built-in magic constants like __FILE__ and __DIR__.
Next, you will explore PHP's data types in depth — the different kinds of values, like strings, integers, and booleans, that variables and constants can hold.
- You can define constants using both define() and const.
- You know the naming convention for constants.
- You understand several of PHP's built-in magic constants.
- You have seen a preview of constants defined inside a class.