Menu

PHP Constants: define() vs const and Magic Constants

A PHP constant is a name for a value that cannot change: const MAX_USERS = 100; or define('MAX_USERS', 100);. Learn define vs const, constant arrays, class constants with self::, checking with defined(), and the magic constants __LINE__, __FILE__, __DIR__ and __CLASS__.

This page includes runnable editors - edit, run, and see output instantly.

A constant is a name for a value that never changes while the script runs. Declare it with const MAX_USERS = 100; or define('MAX_USERS', 100);, then use the bare name, without a $.

By convention constant names are UPPER_SNAKE_CASE. They are global: a constant declared at the top of a file is visible inside every function without global.

define() vs const

Both create a constant, but they work at different times. const is resolved when PHP compiles the file, define() runs like any other function call.

constdefine()
Inside if, loops, functionsNoYes
Name built from a variableNoYes
Class constantsYesNo
Value can be an expressionYes, of constants and literalsYes, any value
Namespaced automaticallyYesNo (give the full name)

Use const by default, because it is shorter and checked at compile time. Use define() when the constant depends on a condition or a computed name.

Constants cannot be changed

Assigning to a constant is a parse error:

<?php
const LIMIT = 10;
LIMIT = 20;
PHP Parse error:  syntax error, unexpected token "=" in /home/index.php on line 3

Defining the same constant twice is not an error, but PHP prints a warning and keeps the first value:

<?php
define("LIMIT", 10);
define("LIMIT", 20);
echo LIMIT; // 10
Warning: Constant LIMIT already defined in /home/index.php on line 3

When a value might already be set (for example in a config file loaded more than once), check with defined() first:

Constant arrays

A constant can hold an array. You can read it with NAME[key], loop over it, and pass it around, but not modify it.

Class constants

Inside a class, const declares a constant that belongs to the class. Read it with ClassName::NAME from outside and self::NAME or static::NAME from inside. Class constants can have visibility, can be final, and since PHP 8.3 can declare a type.

A group of related class constants like STATUS_NEW and STATUS_PAID is often better written as an enum, which also stops invalid values from being passed around.

Magic constants: LINE, FILE, DIR and more

Magic constants change value depending on where they appear. They are filled in at compile time.

The most used one is __DIR__, the directory of the current file. Building include paths from it makes them work no matter which directory the script was started from: require __DIR__ . '/config.php';. See include and require for the full pattern.

Built-in constants worth knowing

PHP defines many constants of its own. These come up all the time:

PHP_EOL is the line ending of the system PHP runs on ("\n" on Linux and macOS, "\r\n" on Windows). It is meant for text written to files and the terminal; in HTML output a newline does not show, so use <br> there.

Frequently Asked Questions

What is the difference between define() and const in PHP?

const is a language construct resolved when the file is compiled: it works at the top level and inside classes, but not inside if, loops or functions. define() is a function called at runtime, so it works anywhere and the name can be built from a variable, but it cannot declare class constants.

Can a PHP constant be an array?

Yes. Both const COLORS = ['red', 'green']; and define('COLORS', ['red', 'green']); work, and you read an element with COLORS[0]. The array cannot be modified afterwards.

How do I check if a constant is defined in PHP?

Use defined('NAME'), which takes the name as a string and returns true or false. A common pattern is if (!defined('APP_ENV')) { define('APP_ENV', 'production'); }. For class constants pass 'ClassName::NAME'.

What are magic constants in PHP?

Constants whose value depends on where they are written: __LINE__ (current line), __FILE__ (full path of the file), __DIR__ (its directory), __FUNCTION__, __CLASS__, __METHOD__ and __NAMESPACE__. They are written with two underscores on each side and are case-insensitive.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED