Skip to main content

How to Define a Constant in PHP with const

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In PHP, const ISSUER = 'https://login.example.org'; defines a constant: a name for a value that can never change. You use it without a dollar sign, as ISSUER, and PHP won't even start a script that tries to assign it a new value.

Syntax​

Syntax
const <NAME> = <value>;
PartWhat it does
constDefines a constant when the script is compiled.
<NAME>The constant's name, by convention in capital letters. It has no $.
<value>A value PHP can work out on its own, such as a string, a number or an array of them.

The PHP manual: "Constants can be defined using the const keyword, or by using the define()-function." and "Once a constant is defined, it can never be changed or undefined."

The Example​

The example defines two constants and prints them. Then a changed version of the script tries to assign a new value to one of them.

Prerequisites​

  • PHP 8. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which defines const ISSUER and const MAX_RETRIES and echoes ISSUER and MAX_RETRIES times 2.

main.php
<?php

const ISSUER = 'https://login.example.org';
const MAX_RETRIES = 3;

echo ISSUER . "\n";
echo MAX_RETRIES * 2 . "\n";

Step 2: Run It​

Terminal
php main.php
Output
https://login.example.org
6

The command is the same in zsh, bash and PowerShell.

Step 3: Try to Change a Constant​

Replace main.php with a version that assigns a new value to ISSUER:

main.php
<?php

const ISSUER = 'https://login.example.org';

ISSUER = 'https://elsewhere.example.org';
echo ISSUER . "\n";
Terminal
php main.php
Output
PHP Parse error: syntax error, unexpected token "=" in /Users/Shared/you/main.php on line 5

Parse error: syntax error, unexpected token "=" in /Users/Shared/you/main.php on line 5

A zsh terminal where php main.php first prints the issuer and 6, then, for the changed file, prints PHP Parse error: syntax error, unexpected token = on line 5, twice.

What Happened​

  • The constants worked like values, written without a $.
  • The changed script didn't run at all. To PHP, ISSUER = ... isn't an assignment it knows, so it stopped while reading the file with syntax error, unexpected token "=". Not even the first line ran.
  • The error appears twice. This PHP's settings both display errors (display_errors is STDOUT) and log them (log_errors is On with no log file, so the log line goes to the terminal too, starting with PHP Parse error:).

Common Mistakes​

MistakeWhat happensDo this instead
Writing a $ in front of the name$ISSUER is a different thing, an undefined variableWrite the bare name: ISSUER
Using const inside a functionThe manual says const constants "must be declared at the top-level scope because they are defined at compile-time"Define them at the top of the file, or use define()
Giving const a value from a function callPHP Fatal error: Constant expression contains invalid operationsUse define() for a value computed at run time

Troubleshooting​

SymptomLikely causeFix
PHP Parse error: syntax error, unexpected token "="Code assigns to a constantUse a variable for a value that changes
Undefined constant "ISSUER"The constant is used before or without its definitionDefine it at the top of the file

FAQ​

What's the difference between const and define()?​

const is part of the language and runs when the file is compiled, so it only works at the top level with fixed values. define('ISSUER', ...) is a function that runs while the script runs, so it also works inside functions and with computed values.

Can a class have constants?​

Yes, with const inside the class, used as ClassName::NAME.

Conclusion​

const NAME = value; at the top of a PHP file defines a constant that can never change, and you use it without a $.

Additional Resources​

  • Syntax, in the PHP manual's chapter on constants.