define() vs const in PHP: Detailed Guide with Examples
\n\nEvery PHP developer sooner or later faces the question: what is the best way to declare a constant — using define() or the const keyword? At first glance, there is almost no difference. Both options create an immutable value. But the devil, as always, is in the details.
This article is not a boring excerpt from the manual. We will analyze real cases where one approach wins and the other fails. You will learn how to properly declare constants in PHP 7 and PHP 8, and be able to write cleaner and more predictable code.
\n\nReady? Let's go!
\n\nWhat is a constant in PHP?
\n\nBefore comparing, let's recall the basics. A constant is an identifier (name) for a simple value. Its main property: the value cannot be changed or deleted during script execution.
\n\nConstants are convenient for storing settings, file paths, API keys, and other static data.
\n\n<?php\n// Defining a constant via define()\ndefine('SITE_NAME', 'PythonLib');\n\n// Defining a constant via const\nconst SITE_URL = 'https://pythonlib.ru';\n\necho SITE_NAME; // PythonLib\necho SITE_URL; // https://pythonlib.ru\n?>\n\nBoth methods work. But let's dig deeper.
\n\nKey differences between define() and const
\n\n1. Time of definition
\n\nThis is the most important point. const is a language construct that is processed at compile time (before script execution). define() is a function that executes at runtime (during script execution).
What does this mean in practice?
\n\n- \n
constcan only be used to declare constants at the top level of a script or inside a class. You cannot placeconstinside aforloop, anifcondition, or a function. \n define()can be called anywhere: inside a loop, condition, or function. This provides flexibility. \n
<?php\n// This will work\nif (true) {\n define('DB_HOST', 'localhost');\n}\n\n// This will cause a fatal error\nif (true) {\n // const DB_HOST = 'localhost'; // Error!\n}\n\n// But const can be used inside a class\nclass Config {\n const DB_NAME = 'test';\n}\n?>\n\nConclusion: if you need to define a constant dynamically (for example, depending on a condition or inside a function), choose define().
2. Data types
\n\nIn PHP 7 and 8, define() supports more data types than const.
- \n
constcan only accept scalar values (int, float, string, bool, null) and arrays (since PHP 5.6). \n define()since PHP 7 also supports arrays, and in PHP 8 — even objects (though this is a rare case). \n
<?php\n// const can store an array (since PHP 5.6)\nconst FRUITS = ['apple', 'banana', 'cherry'];\n\n// define() can too (since PHP 7)\ndefine('VEGETABLES', ['carrot', 'potato']);\n\n// But an object — only via define() (PHP 8+)\n// const OBJECT = new stdClass(); // Error!\ndefine('MY_OBJECT', new stdClass()); // Works\n?>\n\nConclusion: for most tasks, both options are suitable. But if you need to store an object — only define().
3. Context: global vs class constants
\n\nconst is a universal tool. It can be used both outside classes (global constants) and inside classes (class constants).
define() is only for global constants. Inside a class