How to Create Constants with the PHP define Function

09/04/2021

Contents

In this article, you will learn how to create constants with the PHP define function.

PHP define Function

The define() function create a PHP constant.

A constant can store a value, but unlike a variable, once set, it cannot be changed while the program is running.

Constants specified with the define() function can be called from anywhere in the program.
On the other hand, there is a method using const, which allows you to define constants that are only used within a class.

Syntax:

define($name, $value, [case-insensitive])

$name: The name of the constant.

$value: The value of the constant.

[case-insensitive]: If it is set to true, a constant is created with a case-insensitive names.
Default is false.

Sample

Sample Code

<?php
  // case-sensitive constant name
  define("TEXT", "Hello World!");
  echo TEXT;

  echo "<br>";

  // case-insensitive constant name
  define("TEXT", "Hello World!", true);
  echo text;
?>

Result

Hello World!
Hello World!