Skip to content
Theory · Variables and types · lesson 6 of 6

Constants

Some values in a program must never change: the number of days in a week, a tax rate, the limit on login attempts. If you keep such a value in a variable, it can be overwritten by accident in the middle of a file. Then the program silently starts to compute wrong results.

Picture a jar that was sealed right after it was filled. The label is visible, so are the contents, but you can no longer put anything else in. In PHP such a jar is called a constant: a named value that is set once and then only read.

PHP
<?php

const RATE = 3;
define('LIMIT', 10);
echo RATE, ' ', LIMIT, "\n"; // 3 10
echo RATE * LIMIT, "\n"; // 30

Let's go through it line by line:

  1. const RATE = 3; declares the constant RATE with the value 3.
  2. define('LIMIT', 10); declares the constant LIMIT another way—with a function call.
  3. Constants are read by name, without $, and echo prints 3 10.
  4. You can compute with a constant as with an ordinary number: RATE * LIMIT gives 30.

A constant also gives a number a name. The line RATE * LIMIT still makes sense a month later, while with 3 * 10 you have to remember what each number means. And if the rate changes, it is fixed in one place—the declaration.

Two ways to declare

Both ways create the same kind of constant; they differ in how they are written and where they can go.

const

The keyword const, a name, the = sign and a value. It is written at the top level of a file: inside a function, const is a parse error. Modern code uses this form more often.

define

A function with two arguments: the constant's name as a string in quotes, and its value. The constant appears when PHP reaches the call, so define also works inside a function. You will often see this form in older code.

A constant's name is written without $, in capital letters, with words separated by underscores: MAX_LOGIN_ATTEMPTS. Such a name sets a constant apart from a variable at once.

A constant cannot change

You cannot assign a new value to a constant: PHP will not run a file with such a line. Declaring it again does not work either: a second const RATE = 4; gives Warning: Constant RATE already defined, this will be an error in PHP 9, and RATE stays 3.

The jar analogy is not exact here. A sealed jar can be opened with a knife, but a constant has no such knife: its value does not change until the script finishes.

Built-in constants

PHP declares some constants itself:

  • PHP_INT_MAX is the largest number of type int;
  • M_PI is the number π;
  • PHP_EOL is the line break of the system PHP runs on (in this course we write "\n");
  • PHP_VERSION is the PHP version as a string, for example '8.5.12'.
PHP
echo PHP_INT_MAX, "\n"; // 9223372036854775807
echo M_PI, "\n"; // 3.1415926535898
var_dump(PHP_INT_MAX + 1); // float(9.223372036854776E+18)

Let's go through the output:

  1. On a 64-bit system, PHP_INT_MAX is 9223372036854775807.
  2. echo prints a fraction to 14 significant digits, so M_PI is rounded to 3.1415926535898.
  3. PHP_INT_MAX + 1 no longer fits into an int, and the result becomes a float. The notation E+18 means "times 10 to the power of 18".

Interviewers sometimes ask what happens when an integer overflows in PHP. The answer: the result turns into a float and loses precision, but there is no error.

Common mistake

A new value was assigned to a constant, as to a variable:

PHP
const RATE = 3;
RATE = 4; // ✗
TEXT
Parse error: syntax error, unexpected token "="

This is a parse error: PHP does not run the file at all, not even the lines above the error. If a value has to change, it needs a variable: $rate = 4;.

Key points

  • A constant is a named value that is set once: const RATE = 3; or define('LIMIT', 10);.
  • A constant's name is written without $, in capital letters: MAX_LOGIN_ATTEMPTS.
  • You cannot assign a new value to a constant: RATE = 4; is a parse error.
  • PHP declares some constants itself: PHP_INT_MAX, M_PI, PHP_EOL, PHP_VERSION.