Constants
Learn PHP constants — fixed values with define() and const, when to use them instead of variables, and best practices for configuration.
Welcome to Constants
Imagine you're building an e-commerce application.
Some information changes frequently, such as:
- Customer Name
- Product Price
- Order Status
However, some values remain the same throughout the application.
For example:
- Company Name
- Support Email
- Tax Percentage
- Website URL
- Application Version
These values rarely change while your application is running. Instead of storing them in variables, PHP provides a better solution called Constants.
What is a Constant?
A Constant is a named value that cannot be changed once it is created.
Unlike variables, constants keep the same value throughout the execution of the program.
Think of a constant as a permanent label.
For example:
- Company Name
- Country Code
- GST Rate
- PI Value
- Website URL
These values usually remain fixed, making constants the perfect choice.
Variable vs Constant
Let's compare them.
| Variable | Constant |
|---|---|
| Value can change | Value cannot change |
| Starts with $ | No $ symbol |
| Used for dynamic data | Used for fixed data |
| Created using assignment (=) | Created using define() or const |
| Example: Customer Name | Example: Company Name |
Rule of Thumb: If the value is expected to change, use a variable. If the value should always remain the same, use a constant.
Creating a Constant
PHP provides two ways to create constants.
Using define()
<?phpdefine("COMPANY_NAME", "TechLearningPro");echo COMPANY_NAME;?>
Output
TechLearningPro
The first argument is the constant name, and the second is its value.
Using const
<?phpconst WEBSITE = "https://techlearningpro.com";echo WEBSITE;?>
Both approaches create constants. You'll commonly see const used inside classes and define() in configuration files.
Real-World Example
Imagine you're developing an online shopping application.
Some information never changes.
<?phpdefine("COMPANY_NAME", "TechLearningPro");define("SUPPORT_EMAIL", "support@techlearningpro.com");define("GST_RATE", 18);echo COMPANY_NAME;echo "<br>";echo SUPPORT_EMAIL;echo "<br>";echo GST_RATE . "%";?>
Output
TechLearningPro support@techlearningpro.com 18%
If these values need to change in the future, you update them in one place instead of searching through the entire application.
Can a Constant Be Changed?
No. Once a constant is created, its value cannot be modified.
<?phpdefine("PI", 3.14159);PI = 3.14; // Not Allowed?>
PHP will generate an error because constants are read-only. This protects important application settings from accidental changes.
When Should You Use Constants?
Use constants whenever the value should remain fixed.
Examples include:
- Company Name
- Website URL
- Database Version
- Tax Percentage
- Currency Code
- API Version
- Application Environment
Avoid using constants for user-specific information such as names, email addresses, or shopping cart totals.
Best Practices
- Use constants only for values that never change.
- Write constant names in UPPERCASE for better readability.
- Use meaningful names like COMPANY_NAME or MAX_UPLOAD_SIZE.
- Store application configuration values as constants.
- Group related constants together in configuration files.
Common Mistakes
Using a variable instead of a constant
$companyName = "TechLearningPro";
Better
define("COMPANY_NAME", "TechLearningPro");
Trying to modify a constant
define("GST", 18);GST = 20;
A constant cannot be reassigned after it is created.
Using unclear constant names
define("A", 100);
Better
define("MAX_LOGIN_ATTEMPTS", 5);
Meaningful names make your code easier to understand.
Hands-on Exercise
Task: Create constants for Company Name, Website URL, Support Email, and GST Rate. Display all four values on the screen.
Challenge: Imagine you're building a movie ticket booking application. Create constants for CINEMA_NAME, TICKET_BOOKING_FEE, and CUSTOMER_SUPPORT_NUMBER. Print all the values in a formatted output.
<?php// TODO: Taskdefine("COMPANY_NAME", "TechLearningPro");// ...// TODO: Challenge — movie booking constants?>
Summary
- Constants store values that never change during program execution.
- They help keep important configuration values safe and organized.
- Constants do not use the $ symbol.
- You can create constants using define() or const.
- Once created, a constant cannot be modified.
Key Takeaways
- Use variables for changing values.
- Use constants for fixed values.
- Constants improve readability and maintainability.
- Name constants using uppercase letters.
- Protect important application settings with constants.
Interview Questions
Beginner
Q1What is a constant in PHP?
Q2How is a constant different from a variable?
Q3Which symbol is not used with constants?
Q4How do you create a constant using define()?
Q5Can a constant's value be changed?
Intermediate
Q6What is the difference between define() and const?
Q7Why are constants commonly used for configuration values?
Q8When should you choose a constant over a variable?
Advanced
Q9How are class constants different from global constants?
Q10How do constants improve the maintainability of large PHP applications?
What's Next?
Now that you know how to store fixed values using constants, it's time to learn how PHP performs calculations and comparisons.
In the next lesson, Operators, you'll use arithmetic, comparison, and logical operators to work with variables and constants in real application logic.