How to Define a Constant in PHP with const
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
const <NAME> = <value>;
| Part | What it does |
|---|---|
const | Defines 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:

<?php
const ISSUER = 'https://login.example.org';
const MAX_RETRIES = 3;
echo ISSUER . "\n";
echo MAX_RETRIES * 2 . "\n";
Step 2: Run It
php main.php
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:
<?php
const ISSUER = 'https://login.example.org';
ISSUER = 'https://elsewhere.example.org';
echo ISSUER . "\n";
php main.php
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

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 withsyntax error, unexpected token "=". Not even the first line ran. - The error appears twice. This PHP's settings both display errors (
display_errorsisSTDOUT) and log them (log_errorsisOnwith no log file, so the log line goes to the terminal too, starting withPHP Parse error:).
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing a $ in front of the name | $ISSUER is a different thing, an undefined variable | Write the bare name: ISSUER |
Using const inside a function | The 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 call | PHP Fatal error: Constant expression contains invalid operations | Use define() for a value computed at run time |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
PHP Parse error: syntax error, unexpected token "=" | Code assigns to a constant | Use a variable for a value that changes |
Undefined constant "ISSUER" | The constant is used before or without its definition | Define 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.
