PHP Constants
In this page:
define("CONSTANT_NAME", value);
const CONSTANT_NAME = value;
echo CONSTANT_NAME; // no $ sign
Defining Constants with define()
define(SITE_NAME, 'Cookies & Cursor') creates a constant at runtime.
Once set, a constant's value is locked for the rest of the script's execution — there's no reassignment operator for constants the way there is for variables, which is exactly the guarantee you want for values that must never change mid-script.
Example: Defining Constants with define()
<?php
// Call `define('SITE_NAME', 'Cookies & Cursor')`
define('SITE_NAME', 'Cookies & Cursor');
// Print `SITE_NAME` to the output
echo SITE_NAME;
?>
Login to try C/C++/Java/PHP code in the editor
The const Keyword
The const keyword defines a constant at compile time rather than runtime, which makes it slightly faster and is the standard way to declare constants inside a class.
Outside a class, define() and const both work, but const requires the value to be a fixed literal rather than the result of a function call.
Example: The const Keyword
<?php
const MAX_USERS = 100;
// Print `MAX_USERS` to the output
echo MAX_USERS;
?>
Login to try C/C++/Java/PHP code in the editor
Constant Scope
Unlike a variable, a constant is automatically accessible everywhere in the script the moment it's defined — inside every function and every included file — without needing the global keyword.
That global-by-default behavior is exactly why constants suit values like configuration settings or fixed limits.
Example: Constant Scope
<?php
// Call `define('APP_VERSION', '1.0')`
define('APP_VERSION', '1.0');
// Define the function `showVersion` with no parameters
function showVersion() {
// Print `APP_VERSION` to the output
echo APP_VERSION;
}
// Call `showVersion()`
showVersion();
?>
Login to try C/C++/Java/PHP code in the editor
Magic Constants
PHP predefines several 'magic constants' — like __LINE__ and __FILE__ — whose value changes depending on where in your code they appear.
__LINE__ always evaluates to the current line number, and __FILE__ to the full path of the file it's written in, which makes both genuinely useful for logging and debugging.
Example: Magic Constants
<?php
// Print "Line: " . __LINE__ . "\n" to the output
echo "Line: " . __LINE__ . "\n";
// Print `"File: " . __FILE__` to the output
echo "File: " . __FILE__;
?>
Login to try C/C++/Java/PHP code in the editor
Constant Best Practices
Writing constant names in ALL_CAPS isn't enforced by PHP, but it's a near-universal convention that lets a reader instantly tell MAX_RETRIES is a fixed constant rather than a variable that might change.
Grouping related constants together near the top of a file also makes configuration easy to find later.
Example: Constant Best Practices
<?php
// Call `define('MAX_RETRIES', 3)`
define('MAX_RETRIES', 3);
// Call `define('MIN_RETRIES', 1)`
define('MIN_RETRIES', 1);
// Print `MAX_RETRIES` to the output
echo MAX_RETRIES;
?>
Login to try C/C++/Java/PHP code in the editor
- Writing
$before a constant name (echo $SITE_NAME;) or forgetting that constants are used bare, so PHP looks for a variable that does not exist. - Trying to change a constant with
SITE_NAME = New;or redefine it withdefine(), which fails or raises a warning because constants cannot change. - Using
constinside anifor a function, which is not allowed at top level scope only; usedefine()for conditional definitions.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: