How to define constants in PHP

Updated: January 9, 2024 By: Guest Contributor Post a comment

Overview

Defining constants in PHP is essential for creating robust and maintainable applications. This tutorial explores various ways of declaration and best practices.

Introduction to PHP Constants

Constants in PHP are values that, once defined, cannot change during the execution of the script. Unlike variables, they do not start with the $ sign and are usually defined in uppercase letters. The most common method to define a constant is by using the define() function. Here is the simplest way to create a constant:

define('SITE_URL', 'https://www.example.com');

You can also check if a constant has already been defined using defined() function:

if (defined('SITE_URL')) {
    echo 'SITE_URL is defined';
}

Using the const Keyword

PHP also allows for constants to be defined using the const keyword, which behaves slightly differently from define(). One major difference is that const can be used to define constants at compile-time, which means it can be used within a class to define class constants.

const API_KEY = 'abcdef12345';

Class constants can be defined and accessed like this:

class Config {
    const DB_HOST = 'localhost';
}
echo Config::DB_HOST;

Magic Constants

PHP provides a set of predefined constants that change depending on where they are used. These are known as magic constants and look like __CONSTANT_NAME__. For example, __FILE__ returns the full path and filename of the file. Here’s a quick glance at magic constants usage:

echo 'This file is located at ' . __FILE__;

Conditional Constant Declaration

Sometimes, you might want to define constants based on certain conditions. You can combine defined() with define() to conditionally declare a constant:

if (!defined('VERSION')) {
    define('VERSION', '1.0.0');
}

Using Constants in Namespaces

In PHP 5.3 and later, constants can also exist within namespaces which help in avoiding name collisions between code libraries. You can define and access namespaced constants as shown below:

namespace MyProject;

define('MyProject\MAX_USERS', 100);
echo MAX_USERS;

Or using the const keyword:

namespace MyProject {
    const MAX_USERS = 100;
}
echo MyProject\MAX_USERS;

Scalar Type and Array Constants

In PHP 7.0 and higher, you can define array constants with define(). Prior to this version, only scalar data (bool, int, float, string) could be used as constant values:

define('ROLES', ['admin', 'editor', 'subscriber']);
echo ROLES[0]; // Outputs 'admin'

Constant Visibility

const also allows for visibility modifiers as of PHP 7.1, enabling you to define public, private, or protected class constants:

class User {
    public const STATUS_ACTIVE = 'active';
    private const STATUS_PENDING = 'pending';
    protected const STATUS_SUSPENDED = 'suspended';
}

Practical Uses of Constants

Constants are not just for configuration values. They can represent error codes, state flags, important file paths, and more. Using constants can make your code more readable and less prone to errors that can occur when using magic strings or numbers.

Conclusion

In this guide, you’ve learned about different methods and best practices for defining constants in PHP. Constants are a fundamental part of PHP programming that help maintain clean and robust code. Remember to name them clearly and use them consistently throughout your application.