Constants are PHP containers that remain constant and never change. Constants are used for data that is unchanged at multiple places within a program. Variables are temporary storage while Constants are permanent. Use Constants for values that remain fixed and referenced multiple times.
Rules for defining constant
PHP constants can be defined in 2 ways:
- Using define() function
- Using const keyword
Using define() function
define(name, value, case-insensitive)
name: Name of the constant
value: Value of the constant
case-insensitive: Wheather a constant is case-insensitive, The default value is false.
Using const keyword
The const keyword defines constants at compile time. It is a language construct, not a function.
<?php const MESSAGE = "Welcome to PHP"; echo MESSAGE; ?>