W3docs

define()

Learn how PHP define() creates constants, how it differs from const, what values it accepts, and the gotchas around case-insensitivity and array constants.

What the define() Function Does

The define() function creates a constant — a named value that cannot be changed once set. Unlike a variable, a constant has no $ prefix, is global across the entire script (including inside functions), and any attempt to redefine it triggers a warning rather than silently overwriting the value.

Use define() when you want a single source of truth for a value that should never change at runtime: a configuration flag, a version number, a file path, or an API endpoint.

Syntax

define(string $name, mixed $value, bool $case_insensitive = false): bool
  • $name — the constant's name. By convention this is uppercase with underscores (MAX_USERS), but any valid identifier works.
  • $value — the value to store. Scalars (int, float, string, bool) and null have always been allowed; arrays became valid values in PHP 7.0.
  • $case_insensitive (optional) — historically, passing true let you read the constant under any casing. This flag is deprecated since PHP 7.3 and removed in PHP 8.0, so leave it out and treat constant names as case-sensitive.

define() returns true on success and false on failure.

A Basic Example

php— editable, runs on the server

Here define() creates a constant named GREETING holding Hello, world!, then echo prints it by name — no $ and no quotes.

Storing an Array in a Constant

Since PHP 7.0 a constant can hold an array, which is handy for fixed lookup tables or option lists:

<?php
define("ROLES", ["admin", "editor", "viewer"]);
echo ROLES[1]; // editor
?>

Checking Before You Use a Constant

Reading an undefined constant is an error in PHP 8.0+. Guard with defined() so your code degrades gracefully:

<?php
if (!defined("APP_ENV")) {
    define("APP_ENV", "production");
}
echo APP_ENV; // production
?>

define() vs. const

PHP offers two ways to declare a constant, and they are not interchangeable:

Featuredefine()const
When it runsAt runtimeAt compile time
Conditional / loop useYes — can sit inside if/forNo — must be top-level or in a class
Dynamic nameYes — name can be a variableNo — name is fixed
Class constantsNoYes

Reach for define() when the name or value is computed, or when the declaration must be conditional. Use const for simple, always-declared constants and for class constants.

Common Gotchas

  • No $ and no quotes when reading. Write echo GREETING;, not echo $GREETING; or echo "GREETING";.
  • Redefining warns, it doesn't overwrite. A second define("GREETING", ...) emits a warning and the original value stays.
  • Names are case-sensitive. GREETING and Greeting are two different constants now that the case-insensitive flag is gone.
  • Define once, early. Because constants are global and permanent, set them near the top of your script or in a config file.

Conclusion

define() is the runtime way to create a global, immutable value in PHP. Combine it with defined() to avoid redefinition warnings, prefer const for fixed compile-time and class constants, and see PHP Constants for the bigger picture.

Practice

Practice
In PHP, how is the 'define' function used?
In PHP, how is the 'define' function used?
Was this page helpful?