PHP Constants Types in

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

  1. Introduction to PHP Constants

  2. Why Use Constants in PHP

  3. Defining Constants Using define()

  4. Defining Constants Using const

  5. Rules for Naming Constants

  6. Constants vs Variables in PHP

  7. Case Sensitivity in Constants

  8. Checking if a Constant is Defined

  9. Using constant() Function

  10. Magic Constants in PHP

  11. List of All PHP Magic Constants

  12. Global Scope of Constants

  13. Constants in Classes (Class Constants)

  14. Namespaces and Constants

  15. Constants with Arrays

  16. Best Practices for Using Constants

  17. Performance Considerations for Constants

  18. Common Mistakes with Constants

  19. 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:

define("SITE_NAME", "PHP Online");
echo SITE_NAME; // Output: PHP Online

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:

define(name, value, case_insensitive);

Example:

define("SITE_URL", "https://phponline.in");
echo SITE_URL; // Output: https://phponline.in

4. Defining Constants Using const

The const keyword is another way to define constants, mostly used inside classes or for compile-time constants.

Example:

const VERSION = "1.0.0";
echo VERSION;

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).

define("GREETING", "Hello");
echo greeting; // Deprecated behavior

8. Checking if a Constant is Defined

Use defined() function:

if (defined("SITE_URL")) {
echo SITE_URL;
}

9. Using constant() Function

You can get a constant value dynamically:

define("PI", 3.14159);
echo constant("PI");

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:

echo __FILE__;

12. Global Scope of Constants

Constants are automatically global and can be accessed anywhere in the script.


13. Constants in Classes (Class Constants)

class MyApp {
const APP_NAME = "PHP Online";
}
echo MyApp::APP_NAME;

14. Namespaces and Constants

Constants can be defined inside namespaces:

namespace MySite;
const SITE_NAME = "PHP Online";

15. Constants with Arrays

Since PHP 5.6, you can define array constants:

define("COLORS", ["Red", "Green", "Blue"]);
echo COLORS[0];

16. Best Practices for Using Constants

  • Use const inside classes

  • Group 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

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");
define("DB_NAME", "phponline");
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.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments