Constants
Some values should never change — a tax rate, a site name, an HTTP status code. Constants lock a value in place once and make it readable everywhere. This lesson covers const vs define() , array and class constants, and PHP's magic constants.
Learn Constants in our free PHP course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.
Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Defining Constants
A constant is a named value that can never be reassigned. There's no $ , and the convention is UPPER_CASE . You define one with const (modern) or define() (older). Unlike variables, constants are global — readable inside any function without the global keyword.
2️⃣ const vs define()
They look similar but differ in when they run. const is resolved at compile time and must sit at the top level with a literal name. define() is a function called at run time , so it can be conditional or build a name dynamically. Helpers defined() and constant() let you check for and fetch constants by name.
3️⃣ Array Constants
Since PHP 7 a constant can hold an array , and it's fully immutable — perfect for fixed lookup tables, permission lists, or grouped configuration. You can read any element, but any attempt to modify it is a fatal error.
4️⃣ Class Constants
Constants can also live inside a class, where they group related fixed values under a meaningful namespace. Declare them with const inside the class and access them with the :: operator (or self:: from within). Since PHP 7.1 they can carry visibility modifiers like public .
5️⃣ Magic & Predefined Constants
Magic constants change value depending on where they appear — __LINE__ , __FUNCTION__ , __CLASS__ , __FILE__ — and are invaluable for logging and debugging. PHP also ships predefined constants like PHP_VERSION , PHP_EOL , and PHP_INT_MAX .
Now you try. Fill the ___ blanks to set up a small app config.
These scrambled lines should define a rate, use it in a function, and print a result. Put them in the order that prints Tax: 4 .
The constant RATE is defined first so the function can read it (constants are global, so no global keyword is needed). The function must be declared before it's called. 20 * 0.2 gives 4 , so the output is Tax: 4 .
Predict the output before revealing each answer.
Prints 2 . Array constants are allowed (since PHP 7) and you read elements by index just like any array — but you cannot modify them.
Prints no . defined() safely checks whether a constant exists without triggering an error.
Yes. Constants are global by nature, so a function can read SITE_NAME directly — no global keyword required, unlike with variables.
📋 Quick Reference — Constants
Write it yourself, run it on onecompiler.com/php or your own machine, then check against the expected output in the comments.
Practice quiz
Which character must a constant name NOT have?
- An underscore
- An uppercase letter
- A dollar sign $
- A digit
Answer: A dollar sign $. Constants have no dollar sign. Writing $MAX makes it a (probably undefined) variable instead.
What is the key difference between const and define()?
- const is compile-time and top-level; define() runs at run time and can be conditional/dynamic
- define() is faster
- const cannot hold strings
- They are identical
Answer: const is compile-time and top-level; define() runs at run time and can be conditional/dynamic. const is resolved at compile time and must be top-level with a literal name. define() is a run-time function, so it can be conditional or build a name dynamically.
Can a function read a top-level constant without the global keyword?
- No, it needs global
- Only if passed as an argument
- Only inside classes
- Yes — constants have global scope and are visible everywhere
Answer: Yes — constants have global scope and are visible everywhere. Constants are global by nature, so a function can read SITE_NAME directly with no global keyword, unlike variables.