define() vs const in PHP: Battle of the Titans of Constant Declaration
\n\nHave you ever wondered why PHP has two whole ways to declare a constant? It seems like why create extra entities? But the devil, as always, is in the details. This question on Stack Overflow has gathered thousands of views, and for good reason — the difference between define() and const can seriously impact your code architecture.
Today we will once and for all figure out what's what. You will learn not only the syntactic differences but also when each tool gives a real advantage. Let's go!
\n\nA Brief Overview: What is a Constant in PHP?
\n\nA constant is an identifier for a simple value. Its main feature: the value cannot be changed during script execution (unlike a variable). By default, constants are case-sensitive, but this can be configured (spoiler: with define()).
Example of a classic constant:
\n\n<?php\ndefine("MAX_UPLOAD_SIZE", 10485760); // 10 MB in bytes\necho MAX_UPLOAD_SIZE; // 10485760\n?>\n\nThe same thing, but with const:
<?php\nconst MAX_UPLOAD_SIZE = 10485760;\necho MAX_UPLOAD_SIZE;\n?>\n\nSeems the same. But let's dig deeper.
\n\nMain Differences Between define() and const
\n\nLet's break it all down. Here are the key differences every PHP developer needs to know.
\n\n1. Declaration Time: Compilation vs Execution
\n\nThis is the most fundamental difference.
\n\n- \n
const— is declared at the compilation stage. This means you cannot useconstinside loops, conditions, or functions. It must be declared in the "body" of a class (for class constants) or in the global scope. \n define()— is executed at runtime. You can place it anywhere: insideif,for,while, even inside a function (though this is bad practice). \n
An example that will not work with const:
<?php\nif (PHP_OS === 'WINNT') {\n const PLATFORM = 'Windows';\n // Fatal error: Cannot use const in control structure\n}\n?>\n\nBut with define() — no problem:
<?php\nif (PHP_OS === 'WINNT') {\n define('PLATFORM', 'Windows');\n echo PLATFORM; // Windows\n}\n?>\n\n2. Data Types: Arrays and Objects
\n\nFor a long time, define() only supported scalar types (strings, numbers, booleans). Starting from PHP 7, define() learned to work with arrays.
const also supports arrays (since PHP 5.6), but does not support objects or expressions:
<?php\n// Works with PHP 7+\ndefine('FRUITS', ['apple', 'banana', 'cherry']);\necho FRUITS[1]; // banana\n\n// Also works (PHP 5.6+)\nconst COLORS = ['red', 'green', 'blue'];\n\n// But this — error!\n// const RESULT = 2 + 3; // Works, this is a constant expression\n// const DYNAMIC = rand(1, 10); // Error! Not a constant expression\n?>\n\nImportant: define() can use the result of functions (define('SITE_NAME', get_site_name())), while const can only use constant expressions (numbers, strings, other constants).
3. Namespace and Scope
\n\nThere is a subtle but important nuance here.
\n\n- \n
define()— always creates a global constant. Even if you call it inside a namespace, the constant will be accessible from everywhere. \n const— follows namespace rules. A constant declared in the namespaceMyApp\\Configwill be calledMyApp\\Con\n