PHP Constants

PHP Constants – Complete Beginner Guide With Examples
What Are Constants in PHP?
In PHP, a constant is a name (identifier) for a simple value that cannot be changed during script execution.
Unlike variables, constants:
Do not use the
$symbolCannot be redefined
Cannot change their value once set
Constants are commonly used for:
Configuration settings
Database credentials
Fixed values like tax rates
Application settings
Example:
Output:
Difference Between Variables and Constants
| Feature | Variable | Constant |
|---|---|---|
| Symbol | Uses $ | No $ |
| Value Change | Can change | Cannot change |
| Scope | Depends | Global by default |
| Declaration | $x = 10; | define() or const |
Constants are best for fixed values.
How to Create Constants in PHP
There are two main ways to create constants:
Using
define()functionUsing
constkeyword
Let’s understand both.
Using define() Function
The define() function is used to create constants.
Syntax:
Example:
Output:
Important:
Constant names are usually written in uppercase (best practice).
Names are case-sensitive by default.
Using const Keyword
The const keyword can also define constants.
Example:
Difference Between define() and const
| Feature | define() | const |
|---|---|---|
| Works Outside Class | Yes | Yes |
| Works Inside Class | Yes | Yes |
| Can Use in Conditions | Yes | No |
| Compile-Time Constant | No | Yes |
For simple scripts, both work fine.
Constant Naming Rules
When creating constants:
- Must start with a letter or underscore
- Cannot start with a number
- No
$symbol - Case-sensitive (by default)
Valid:
Invalid:
Accessing Constants
Unlike variables, constants are accessed directly by name.
No $ required.
Constants Are Global
Constants are automatically global.
Example:
Output:
No need to use global.
Magic Constants in PHP
PHP provides built-in predefined constants called Magic Constants.
They change depending on where they are used.
LINE
Returns current line number.
FILE
Returns full file path.
DIR
Returns directory of file.
FUNCTION
Returns function name.
CLASS
Returns class name.
METHOD
Returns class method name.
Predefined PHP Constants
PHP provides many built-in constants.
Examples:
PHP_VERSION
PHP_OS
PHP_INT_MAX
E_ERROR
TRUE
FALSE
Example:
Displays installed PHP version.
Constants in Classes
You can define constants inside classes.
Example:
Access using:
Real-World Example – Database Configuration
Constants are commonly used for configuration.
This prevents accidental modification.
Why Use Constants?
Constants are useful because:
- They protect important values
- Improve code readability
- Reduce accidental changes
- Ideal for configuration settings
- Automatically global
Can Constants Store Arrays?
Yes (in modern PHP versions).
Or using const:
Checking if Constant Exists
Use defined().
Common Beginner Mistakes
Using $ with Constants
Wrong:
Correct:
Trying to Redefine Constant
This will cause an error.
Using Lowercase Constant Names
Best practice is uppercase:
Best Practices for Using Constants
- Use uppercase naming
- Use constants for fixed values
- Use for configuration settings
- Avoid redefining
- Use descriptive names
Variable vs Constant Example
Use variable when value may change.
Use constant when value must remain fixed.
Why PHP Constants Are Important
Constants are essential for:
Application settings
Security configuration
Database credentials
API keys
Fixed rates and values
Without constants, maintaining large applications becomes difficult.
FAQs About PHP Constants
1. What is a constant in PHP?
A constant is a fixed value that cannot be changed once defined.
2. How do you define a constant in PHP?
Using define() function or const keyword.
3. Can constants be changed?
No, constants cannot be modified after definition.
4. What is the difference between const and define()?
const is a compile-time constant, while define() is a runtime function.
5. Are PHP constants global?
Yes, constants are automatically global.
Conclusion
PHP constants are essential for storing fixed values that should not change during program execution.
By understanding:
How to define constants
Difference between define() and const
Magic constants
Class constants
Best practices
You can write safer, more maintainable PHP applications.
Constants are especially useful for configuration settings and fixed values.
