Skip to content

PHP Tutorial Variables and Constants

The const Keyword

Lesson 26 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

const is the simplest way to create a constant.

index.php
PHP
<?php
const APP_NAME = "Task Manager";
const MAX_ITEMS = 50;

echo APP_NAME . " allows " . MAX_ITEMS . " items.";
Output
OUTPUT
Task Manager allows 50 items.

Constants can hold arrays

index.php
PHP
<?php
const ROLES = ["admin", "editor", "viewer"];

echo ROLES[1];
Output
OUTPUT
editor

Constants can use other constants

index.php
PHP
<?php
const SECONDS_PER_MINUTE = 60;
const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60;

echo SECONDS_PER_HOUR;
Output
OUTPUT
3600

The value must be something PHP can work out before the script runs, so a call to a normal function (such as date()) is not allowed in a const.

const inside classes

Class constants use const too. This is where you will use it most.

index.php
PHP
<?php
class Order
{
    const STATUS_PAID = "paid";
    const STATUS_PENDING = "pending";
}

echo Order::STATUS_PAID;
Output
OUTPUT
paid

From PHP 8.3 you can add a type to a class constant, for example const string STATUS_PAID = "paid";. On older versions, that is a parse error.

Rules of const

  • It must be written at the top level of the file or inside a class. It cannot sit inside an if or a loop.
  • The name is fixed when the file is compiled.

When you need a constant with a computed name or inside an if, use define() instead.

lightbulb
Default to const
In current code, const is the normal choice. Reach for define() only when you need to build the name or value at runtime.