define() vs const in PHP: What to Choose in 2024 (+code)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

define() vs const in PHP: Detailed Guide with Examples

\n\n

Every 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.

\n\n

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\n

Ready? Let's go!

\n\n

What is a constant in PHP?

\n\n

Before 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\n

Constants 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\n

Both methods work. But let's dig deeper.

\n\n

Key differences between define() and const

\n\n

1. Time of definition

\n\n

This 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).

\n\n

What does this mean in practice?

\n\n
    \n
  • const can only be used to declare constants at the top level of a script or inside a class. You cannot place const inside a for loop, an if condition, or a function.
  • \n
  • define() can be called anywhere: inside a loop, condition, or function. This provides flexibility.
  • \n
\n\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\n

Conclusion: if you need to define a constant dynamically (for example, depending on a condition or inside a function), choose define().

\n\n

2. Data types

\n\n

In PHP 7 and 8, define() supports more data types than const.

\n\n
    \n
  • const can 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
\n\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\n

Conclusion: for most tasks, both options are suitable. But if you need to store an object — only define().

\n\n

3. Context: global vs class constants

\n\n

const is a universal tool. It can be used both outside classes (global constants) and inside classes (class constants).

\n\n

define() is only for global constants. Inside a class

Blogs

Book Recommendations