PHP Constants: A Complete Step-by-Step Guide

PHP constants are immutable values that do not change during script execution. They are useful for storing fixed values like configuration settings, database credentials, or mathematical constants.

1. What Are PHP Constants?

A constant in PHP is a name assigned to a value that cannot be changed after it has been defined.

Key Features of Constants:

✅ Values remain unchanged throughout execution
✅ No $ symbol before the name (unlike variables)
✅ Automatically global across the entire script

2. How to Define Constants in PHP

Syntax:

define("CONSTANT_NAME", "value");

Example:

<?php
    define("SITE_NAME", "W3HTMLSchool");
    echo SITE_NAME; // Outputs: W3HTMLSchool
?>

3. Defining Constants Using const (Alternative Method)

PHP also allows defining constants using the const keyword:

<?php
    const SITE_URL = "https://w3htmlschool.com";
    echo SITE_URL;
?>

Key Differences Between define() and const:

Featuredefine()const
Can be used inside functions✅ Yes❌ No
Supports dynamic values✅ Yes❌ No
Used inside classes✅ Yes✅ Yes (PHP 5.6+)

4. Constant Naming Rules

✅ Must start with a letter or underscore
✅ Should use uppercase letters (best practice)
✅ Cannot contain spaces

Valid Names:
✔️ SITE_NAME
✔️ _VERSION

Invalid Names:
1CONSTANT (Cannot start with a number)
site name (Spaces not allowed)

5. PHP Constants vs. Variables

FeatureConstants (define())Variables ($var)
Changeable❌ No✅ Yes
Starts with $❌ No✅ Yes
Global Scope✅ Yes❌ No (Local by default)
Memory Efficient✅ Yes❌ No

Example:

<?php
    define("PI", 3.1416);  // Constant
    $radius = 5;           // Variable
    $area = PI * $radius * $radius;
    echo "Area: " . $area;
?>

6. Using Constants Inside Functions

Since constants have a global scope, they can be used inside functions without declaring them as global.

<?php
    define("GREETING", "Welcome to PHP!");

    function sayHello() {
        echo GREETING;
    }

    sayHello(); // Outputs: Welcome to PHP!
?>

7. Predefined PHP Constants

PHP comes with many built-in constants that are useful for various operations.

Common Predefined Constants:

ConstantDescription
PHP_VERSIONCurrent PHP version
PHP_OSOperating system name
E_ERRORError type constant
PHP_INT_MAXMaximum integer value

Example:

<?php
    echo "PHP Version: " . PHP_VERSION;
    echo "OS: " . PHP_OS;
?>

8. Class Constants in PHP

Constants can also be used inside classes using the const keyword.

<?php
    class Car {
        const BRAND = "Toyota";
    }

    echo Car::BRAND; // Outputs: Toyota
?>

9. Checking if a Constant is Defined

Use the defined() function to check whether a constant exists.

<?php
    define("API_KEY", "12345");

    if (defined("API_KEY")) {
        echo "API_KEY is defined";
    } else {
        echo "API_KEY is not defined";
    }
?>

10. Best Practices for Using Constants in PHP

✅ Use uppercase names for constants (DB_HOST, MAX_USERS)
✅ Group related constants together in configuration files
✅ Avoid using magic numbers—use named constants instead
✅ Always check if a constant exists before using it