Open In App

What is the Difference Between define() and const Keyword in PHP ?

In PHP, both define( ) and const Keywords are used to define constants, which are identifiers with unchanging values. However, they have differences in terms of usage, scope, and flexibility.

Syntax

// Using define() function
define("CONSTANT_NAME", value);

// Using const keyword (outside of class)
const CONSTANT_NAME = value;

Important Points

Difference between define() and const

define() const keyword
Defines constants at runtime Defines class constants at compile time
Can be defined anywhere in the script Can only be defined within classes or interfaces
Case-insensitive by default Case-sensitive by default
Global scope Class scope

Usage

Example:

// Using define() to define a Global constant
define("PI", 3.14);

// Using const keyword to define a Class constant
class Math {
const PI = 3.14;
}
Article Tags :