📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners PHP Constants

PHP Constants

4 min read Quiz at the end
Constants hold values that never change during the script. Define them with define() or const and write them in UPPERCASE by convention. Use constants for things like database names, API base URLs, and maximum upload sizes.

Constants

Constants hold values that never change. Unlike variables, they have no $ prefix.

// define() — works anywhere
define("MAX_SIZE", 100);
define("SITE_URL", "https://example.com");

// const — cleaner, works at class/global scope
const PI = 3.14159;
const APP_NAME = "MyApp";

echo MAX_SIZE;    // 100
echo PI;          // 3.14159

// PHP built-in constants
echo PHP_VERSION;   // 8.x.x
echo PHP_EOL;       // newline
echo PHP_INT_MAX;   // largest int
echo __FILE__;      // current file path
echo __LINE__;      // current line number
Topic Quiz · 5 questions

Test your understanding before moving on

1. How do you define a constant in PHP?
💡 Constants are defined with const NAME = value; or define("NAME", value);
2. Can a constant be changed after definition?
💡 Constants cannot be changed or undefined after they are set.
3. Which built-in constant gives the current line number?
💡 __LINE__ is a magic constant that returns the current line number.
4. What is the difference between const and define()?
💡 const is evaluated at compile time and works in class scope; define() is runtime.
5. Which magic constant returns the current file path?
💡 __FILE__ returns the full path and filename of the current file.