define() vs const in PHP: What to Choose and Why It Matters

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: Battle of the Titans of Constant Declaration

\n\n

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

\n\n

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

A Brief Overview: What is a Constant in PHP?

\n\n

A 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()).

\n\n

Example of a classic constant:

\n\n
<?php\ndefine("MAX_UPLOAD_SIZE", 10485760); // 10 MB in bytes\necho MAX_UPLOAD_SIZE; // 10485760\n?>
\n\n

The same thing, but with const:

\n\n
<?php\nconst MAX_UPLOAD_SIZE = 10485760;\necho MAX_UPLOAD_SIZE;\n?>
\n\n

Seems the same. But let's dig deeper.

\n\n

Main Differences Between define() and const

\n\n

Let's break it all down. Here are the key differences every PHP developer needs to know.

\n\n

1. Declaration Time: Compilation vs Execution

\n\n

This is the most fundamental difference.

\n\n
    \n
  • const — is declared at the compilation stage. This means you cannot use const inside 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: inside if, for, while, even inside a function (though this is bad practice).
  • \n
\n\n

An example that will not work with const:

\n\n
<?php\nif (PHP_OS === 'WINNT') {\n    const PLATFORM = 'Windows';\n    // Fatal error: Cannot use const in control structure\n}\n?>
\n\n

But with define() — no problem:

\n\n
<?php\nif (PHP_OS === 'WINNT') {\n    define('PLATFORM', 'Windows');\n    echo PLATFORM; // Windows\n}\n?>
\n\n

2. Data Types: Arrays and Objects

\n\n

For a long time, define() only supported scalar types (strings, numbers, booleans). Starting from PHP 7, define() learned to work with arrays.

\n\n

const also supports arrays (since PHP 5.6), but does not support objects or expressions:

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

Important: define() can use the result of functions (define('SITE_NAME', get_site_name())), while const can only use constant expressions (numbers, strings, other constants).

\n\n

3. Namespace and Scope

\n\n

There 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 namespace MyApp\\Config will be called MyApp\\Con
  • \n

Blogs

Book Recommendations