PHP Lesson 15 – Constants

Last updated on November 29th, 2023

What is the difference between variables and constants in PHP?

In PHP, variables and constants are both used to store data, but they serve different purposes and have distinct characteristics. Here are the key distinctions between the two:

Mutability: The most significant difference between variables and constants is that variables can be changed during the execution of the script, while constants cannot. Once a constant is defined, its value remains constant and cannot be altered, ensuring that it remains consistent throughout the script. On the other hand, the value of a variable can be modified at any point during the script execution.

Declaration and Usage: Variables are declared using the dollar sign ($) followed by a name, and they can hold various data types, including integers, floats, strings, arrays, and objects. They can be assigned values, modified, and reassigned as needed. Constants, on the other hand, are typically declared using the define() function or the const keyword, and their names are conventionally written in uppercase letters. Constants can hold only scalar data types (like integers, floats, booleans, and strings), and they cannot be used to store arrays or objects.

Scope: Variables can have different scopes, including global scope, function scope, and class scope, depending on where they are declared. Constants are global in scope by default, meaning they can be accessed from anywhere within the script. This can lead to namespace pollution if not used carefully.

Dynamic Nature: Variables can be dynamically assigned values during the execution of the script, making them suitable for situations where the value needs to change. Constants, on the other hand, are typically used for values that remain fixed throughout the execution of the script, providing a way to define a value that should not be altered.

Naming Conventions: While variables typically use lowercase letters and underscores for multi-word names (e.g., $my_variable_name), constants are conventionally written in uppercase letters, with words separated by underscores (e.g., MY_CONSTANT_NAME).

How to define a constant?

For example, if you have a value that represents the tax rate in your application and you want to ensure that this value remains the same throughout your code, you can define it as a constant. This way, you can use the constant name wherever you need to use the tax rate, making your code more readable and maintainable.

Constants can be defined using the define() function or the const keyword. Here’s an example of how you can define a constant in PHP:

// Using the define() function
define(‘TAX_RATE’, 0.1);

// Using the const keyword (introduced in PHP 5.3.0)

In this example, TAX_RATE and MAX_LOGIN_ATTEMPTS are constant identifiers that hold specific values. Once defined, you can use these constants throughout your PHP script without worrying about accidental changes to their values.

If you use the define function, the constant name must be enclosed in quotation marks, and there must be a comma between the constant name and its value. With both variants (define and const), the construct must end with a semicolon.

Examples of using PHP constants

Using constants for defining configuration values.


// Define a constant for the website name
define('WEBSITE_NAME', 'My Website');

// Print the constant value
echo "Welcome to " . WEBSITE_NAME;


In this example, the constant WEBSITE_NAME is defined with the value ‘My Website’. It is then used to display a welcome message, demonstrating how constants can be used to store and retrieve configuration values.

Using constants for mathematical calculations.


// Define a constant for the value of pi
define('PI_VALUE', 3.14159);

// Calculate the area of a circle
$radius = 5;
$area = PI_VALUE * $radius * $radius;

// Display the calculated area
echo "The area of the circle is: " . $area;


Here, the constant PI_VALUE is defined with the value 3.14159, representing the mathematical constant Pi. It is then used in the calculation of the area of a circle based on the provided radius, demonstrating how constants can be utilized in mathematical computations.

Using constants for error messages.


// Define constants for error messages
define('ERROR_MESSAGE_404', 'Error 404: Page not found');
define('ERROR_MESSAGE_500', 'Error 500: Internal server error');

// Simulate an error scenario
$errorCode = 404;

// Display the corresponding error message
if ($errorCode === 404) {
    echo ERROR_MESSAGE_404;
} elseif ($errorCode === 500) {
    echo ERROR_MESSAGE_500;
} else {
    echo "Unknown error";


This example demonstrates how constants can be used to store predefined error messages. Depending on the value of the $errorCode variable, the corresponding error message is displayed, showcasing the utility of constants in managing and displaying specific predefined values.