Skip to content
SW Study Warehouse
Home › Blog
Blog

Constants in PHP: define() vs const Explained (2026 Guide)

By Pardeep Patel · November 25, 2026 · 2 min read 👁 126 Views
PhP
Disclosure: Some links in this guide are affiliate links. We may earn a commission at no extra cost to you.

Understanding constants in php is an essential building block for beginners and students writing clean, reliable code. Below you will find the step-by-step syntax, working example, and key rules to follow.

Complete Guide: Constants In Php

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
Syntax define(name, value, case-insensitive)
  • Constants are PHP container that remains constant and never change
  • Constants are used for data that is unchanged at multiple places within our program
  • Variables are temporary storage while Constants are permanent
  • Use Constants for values that remain fixed and referenced multiple times

Parameters

  • name: Specifies the name of the constant
  • value: Specifies the value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

Valid and Invalid Constant declaration

<?php
//valid constant names
define('ONE', "first value");
define('TWO', "second value");
define('SUM 2',ONE+TWO);

//invalid constant names
define('1ONE', "first value");
define(' TWO', "second value");
define('@SUM',ONE+TWO);
?>

Constants are Global

Constants are automatically global and can be used across the entire script. The example below uses a constant inside a function, even if it is defined outside the function:
<?php
define("GREETING", "Welcome to studywarehouse.com!");

function myTest() {
    echo GREETING;
}
 
myTest();
?>
 

Key Takeaways & Official Reference for Constants In Php

When practicing constants in php, test edge cases carefully and keep your formatting consistent. For deeper reference, review the Official PHP Manual or continue with our Basic Syntax of PHP.

Found this guide helpful? Support Team StudyWarehouse! Buy Us a Coffee

Pardeep Patel

Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Leave a Reply

Your email address will not be published. Required fields are marked *