Table of Contents:
PHP Constants Complete Guide | Define and Use Constants in PHP
At phponline.in, our goal is to make learning PHP easy, beginner-friendly, and SEO-rich. In this guide, we will cover everything about PHP constants — from what they are, how to define them, how they differ from variables, to advanced uses, magic constants, and best practices.
PHP Constants – Complete Beginner to Advanced Guide
You will Learn
Introduction to PHP Constants
Why Use Constants in PHP
Defining Constants Using
define()
Defining Constants Using
const
Rules for Naming Constants
Constants vs Variables in PHP
Case Sensitivity in Constants
Checking if a Constant is Defined
Using
constant()
FunctionMagic Constants in PHP
List of All PHP Magic Constants
Global Scope of Constants
Constants in Classes (Class Constants)
Namespaces and Constants
Constants with Arrays
Best Practices for Using Constants
Performance Considerations for Constants
Common Mistakes with Constants
Real-World Examples of Constants
1. Introduction to PHP Constants
A constant in PHP is a name or an identifier for a simple value. Unlike variables, constants cannot be changed after they are defined.
Example:
2. Why Use Constants in PHP
Constants are used for values that should never change during script execution, such as:
Website name
API keys
Database credentials
Configuration values
Application-wide settings
3. Defining Constants Using define()
The define()
function is the traditional way of creating constants in PHP.
Syntax:
Example:
4. Defining Constants Using const
The const
keyword is another way to define constants, mostly used inside classes or for compile-time constants.
Example:
5. Rules for Naming Constants
Use uppercase letters with underscores:
MAX_USERS
Cannot start with a number
No spaces allowed
Should be descriptive
6. Constants vs Variables in PHP
Feature | Constants | Variables |
---|---|---|
Can change value? | No | Yes |
Start with $ ? | No | Yes |
Scope | Global | Varies |
Storage | Fixed | Dynamic |
7. Case Sensitivity in Constants
By default, constants are case-sensitive. You can make them case-insensitive using define()
‘s third parameter (deprecated in PHP 8.0).
8. Checking if a Constant is Defined
Use defined()
function:
9. Using constant()
Function
You can get a constant value dynamically:
10. Magic Constants in PHP
Magic constants change depending on where they are used.
11. List of All PHP Magic Constants
Constant | Description |
---|---|
__LINE__ | Current line number |
__FILE__ | Full path and filename |
__DIR__ | Directory of the file |
__FUNCTION__ | Function name |
__CLASS__ | Class name |
__TRAIT__ | Trait name |
__METHOD__ | Method name |
__NAMESPACE__ | Current namespace |
Example:
12. Global Scope of Constants
Constants are automatically global and can be accessed anywhere in the script.
13. Constants in Classes (Class Constants)
14. Namespaces and Constants
Constants can be defined inside namespaces:
15. Constants with Arrays
Since PHP 5.6, you can define array constants:
16. Best Practices for Using Constants
Use
const
inside classesGroup related constants together
Name constants descriptively
Avoid magic numbers
17. Performance Considerations for Constants
Constants are faster than global variables for fixed values
Avoid redefining constants in loops
18. Common Mistakes with Constants
Trying to change constant values after definition
Using undefined constants
Defining constants inside conditional blocks incorrectly
19. Real-World Examples of Constants
Example: Config File
PHP constants, PHP define, PHP const, PHP magic constants, PHP constant scope, PHP constant example, PHP define vs const, PHP constant naming
Learn more
Frequently Asked Questions (FAQs)
Q1: What is the difference between define()
and const
?define()
is used at runtime, const
at compile-time and inside classes.
Q2: Are PHP constants case-sensitive?
Yes, unless explicitly made case-insensitive (deprecated in PHP 8.0).
Q3: Can a constant store an array?
Yes, from PHP 5.6 onwards.
Q4: Are constants faster than variables?
Yes, for fixed values, constants are slightly faster.
Q5: Can I unset a constant?
No, constants cannot be unset or redefined.