AdyBlaze
Latest Article

PHP Constants Explained with Examples (Beginner Guide 2026)

23 Jul 2026

WhatsApp Facebook Telegram

What are Constants in PHP?

In PHP, a constant is a value that cannot be changed once it has been created.

Unlike variables, whose values can change during program execution, constants remain fixed throughout the script.

Constants are useful for storing values that should never change, such as:

Using constants makes your code cleaner, more secure, and easier to maintain.


Why Use Constants?

Imagine your website name appears in 100 different files.

Instead of writing:

echo "AdyBlaze";

everywhere, you can create a constant once:

define("SITE_NAME", "AdyBlaze");

Now you only need:

echo SITE_NAME;

If the website name changes, you update it in one place.


Creating Constants using define()

The most common way to create a constant is with the define() function.

Example:

<?php

define("SITE_NAME", "AdyBlaze");

echo SITE_NAME;

?>

Output

AdyBlaze

Understanding define()

Syntax:

define("CONSTANT_NAME", value);

Constant names are usually written in UPPERCASE.

Example:

define("APP_VERSION", "1.0");
define("GST", 18);
define("COMPANY", "AdyBlaze");

Creating Constants using const

PHP also supports the const keyword.

Example:

<?php

const COUNTRY = "India";

echo COUNTRY;

?>

Output

India

define() vs const

define()const
FunctionLanguage keyword
Can be declared anywhereUsually declared outside functions
Value can be dynamicValue should be constant
Commonly used in configuration filesCommonly used inside classes

Constants vs Variables

Example:

<?php

$name = "Adil";

define("SITE", "AdyBlaze");

$name = "Rahul";

echo $name;

echo "<br>";

echo SITE;

?>

Output

Rahul
AdyBlaze

Notice that the variable changed, but the constant remained the same.


Constant Naming Rules

Good examples:

SITE_NAME

APP_VERSION

MAX_UPLOAD_SIZE

DATABASE_NAME

Avoid:

siteName

abc

x

test123

Meaningful names make your code easier to understand.


Real-World Example

A website configuration file often contains constants.

<?php

define("SITE_NAME", "AdyBlaze");

define("ADMIN_EMAIL", "admin@adyblaze.com");

define("SUPPORT_EMAIL", "support@adyblaze.com");

define("DEFAULT_LANGUAGE", "English");

?>

These values are accessed throughout the application.


Magic Constants

PHP includes several predefined constants known as Magic Constants.

Some useful ones include:

Current File

echo __FILE__;

Returns the full path of the current file.


Current Directory

echo __DIR__;

Returns the current directory.


Current Line Number

echo __LINE__;

Displays the current line number.


Current Function Name

echo __FUNCTION__;

Useful while debugging functions.


Current Class Name

echo __CLASS__;

Returns the current class name.


Best Practices


Common Mistakes


Frequently Asked Questions

Can a constant be changed?

No. Once created, its value cannot be modified.


Which is better: define() or const?

Both are useful.


Are constants global?

Yes. Once declared, they can be accessed throughout the script.


Conclusion

Constants make PHP applications more organized by storing fixed values that never change. They improve code readability, simplify maintenance, and reduce the chances of accidental modifications.

As your projects grow, using constants for configuration and application settings becomes an essential best practice.


Next Tutorial

PHP Comments Explained with Examples (2026 Guide)