PHP Constants

Summary: in this tutorial, you learn about PHP constants and how to use the define() and const keyword to define constants.

Introduction to PHP constants

A constant is simply a name that holds a simple value. As its name implies, the value of a constant cannot be changed during the execution of the script.

To define a constant, you use the  define() function. The  define() function takes the name of the constant as the first argument and the constant value as the second argument. For example:

<?php define('WIDTH','1140px'); echo WIDTH;
Code language: PHP (php)

By convention, constant names are uppercase. Unlike a variable, the constant name doesn’t start with the dollar sign($).

By default, constant names are case-sensitive. It means that WIDTH and width are different constants.

It’s possible to define case-insensitive constants. However, it’s deprecated since PHP 7.3.0

In PHP 5, a constant can hold a simple value like a number, a string, a boolean value. From PHP 7, a constant can hold an array. For example:

<?php define( 'ORIGIN', [0, 0] );
Code language: PHP (php)

Like superglobal variables, constants can be accessed from anywhere in the script.

The const keyword

PHP provides you with another way to define a constant via the const keyword. Here’s the syntax:

const CONSTANT_NAME = value;
Code language: PHP (php)

In this syntax, you define the constant name after the const keyword. To assign a value to a constant, you use the assignment operator (=) and the constant value. The constant value can be scalar e.g., a number, a string, or an array.

The following example uses the const keyword to define the SALES_TAX constant:

<?php const SALES_TAX = 0.085; $gross_price = 100; $net_price = $gross_price * (1 + SALES_TAX); echo $net_price; // 108.5
Code language: PHP (php)

The following example uses the const keyword to define the RGB constant that holds an array:

<?php const RGB = ['red', 'green', 'blue'];
Code language: PHP (php)

define vs const

First, the define() is a function while the const is a language construct.

It means that the define() function defines a constant at run-time whereas the const keyword defines a constant at compile time.

In other words, you can use the define() function to define a constant conditionally like this:

<?php if(condition) { define('WIDTH', '1140px'); }
Code language: PHP (php)

However, you cannot use the const keyword to define a constant this way. For example, the syntax of the following code is invalid:

<?php if(condition) { const WIDTH = '1140px'; }
Code language: PHP (php)

Second, the define() function allows you to define a constant with the name that comes from an expression. For example, the following defines three constants PREFIX_1, PREFIX_2, and PREFIX_3 with the values 1, 2, and 3.

<?php define('PREFIX', "OPTION"); define(PREFIX . '_1', 1); define(PREFIX . '_2', 2); define(PREFIX . '_3', 3);
Code language: PHP (php)

However, you cannot use the const keyword to define a constant name derived from an expression.

Unless you want to define a constant conditionally or using an expression, you can use the const keyword to define constants to make the code more clear.

Note that you can use the const keyword to define constants inside classes.

Summary

  • A constant is a name that holds a simple value that cannot be changed during the execution of the script. From PHP 7, a constant can hold an array.
  • A constant can be accessed from anywhere in the script.
  • Use the define() function or const keyword to define a constant.
  • Use the define() function if you want to define a constant conditionally or using an expression.
Did you find this tutorial useful?