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
const RATE = 3;
define('LIMIT', 10);
echo RATE, ' ', LIMIT, "\n"; // 3 10
echo RATE * LIMIT, "\n"; // 30Let's go through it line by line:
const RATE = 3;declares the constantRATEwith the value3.define('LIMIT', 10);declares the constantLIMITanother way—with a function call.- Constants are read by name, without
$, andechoprints3 10. - You can compute with a constant as with an ordinary number:
RATE * LIMITgives30.
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_MAXis the largest number of typeint;M_PIis the number π;PHP_EOLis the line break of the system PHP runs on (in this course we write"\n");PHP_VERSIONis the PHP version as a string, for example'8.5.12'.
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:
- On a 64-bit system,
PHP_INT_MAXis9223372036854775807. echoprints a fraction to 14 significant digits, soM_PIis rounded to3.1415926535898.PHP_INT_MAX + 1no longer fits into anint, and the result becomes afloat. The notationE+18means "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:
const RATE = 3;
RATE = 4; // ✗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;ordefine('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.