PHP const Keyword: Syntax, Examples & Usage

1. Introduction

In PHP Object-Oriented Programming (OOP), the const keyword is used to define class constants. A constant is a fixed value that cannot be changed once it has been declared.

OR

A constant is a value that cannot be modified once it is defined.

Constants are useful when you want to define values that should remain the same during the execution of the program. For example:

  • Company name
  • Website URL
  • Configuration values
  • System settings

In PHP classes, constants are declared using the const keyword.

πŸ“–
Important Characteristics of Constants:
  • Constants must be defined inside a class when using the const keyword in OOP.
  • The value of a constant cannot be changed after it is defined.
  • Constants do not use the dollar sign ($) like variables.
  • Constants are accessed using the Scope Resolution Operator (::).
  • Inside the class, constants are accessed using the self keyword.

2. Syntax of Const Keyword

The basic syntax for defining a constant inside a class is:


class ClassName {
   const CONSTANT_NAME = "Value";
}

Accessing Constant Inside the Class

Inside the class, the constant can be accessed using:


self::CONSTANT_NAME;

Accessing Constant Outside the Class

Outside the class, constants are accessed using:


ClassName::CONSTANT_NAME;

The :: symbol is called the Scope Resolution Operator.

3. Example of PHP Const Keyword

Let us look at a simple example of defining and accessing constants in PHP.

Example


class Employee {
   const shift = "Office timing is 10am to 6pm.";
   const company_name = "Company name is TCS.";
   function displayShiftTiming() {
       echo self::shift;
   }
}
echo Employee::company_name;
$emp_obj = new Employee;
$emp_obj->displayShiftTiming();

4. Output

Company name is TCS. Office timing is 10am to 6pm

5. Explanation

Let us understand how the code works step by step.

Step 1: Creating the Class

First, we create a class called Employee.


class Employee

This class will contain constants and methods related to employees.

Step 2: Defining Constants

Inside the class we define two constants.


const shift = "Office timing is 10am to 6pm.";
const company_name = "Company name is TCS.";

Important points:

  1. We use the const keyword to define constants.
  2. Constants do not use $ sign.
  3. Once defined, their values cannot be changed.

Step 3: Accessing Constant Inside Class

Inside the method displayShiftTiming(), we access the constant using:


self::shift;

here:

  • self refers to the current class
  • :: is the scope resolution operator

So this statement accesses the constant shift inside the class.

Step 4: Accessing Constant Outside Class

Outside the class we access the constant using:


Employee::company_name;

Here:

  • Employee is the class name
  • :: allows access to class constants

This prints the company name.

Step 5: Creating Object

Next we create an object of the class.


$emp_obj = new Employee;

Then we call the method:


$emp_obj->displayShiftTiming();

This prints the office timing.

6. Real-World Example

Let us see a practical example where constants are used in a website configuration class.

Example


class WebsiteConfig {
   const WEBSITE_NAME = "TechFliez";
   const SUPPORT_EMAIL = "support@techfliez.com";
   const VERSION = "1.0";
   function displayWebsiteInfo() {
       echo "Website: " . self::WEBSITE_NAME;
       echo "
"; echo "Support Email: " . self::SUPPORT_EMAIL; echo "
"; echo "Version: " . self::VERSION; } } $config = new WebsiteConfig; $config->displayWebsiteInfo();

Output

Website: TechFliez
Support Email: support@techfliez.com
Version: 1.0

Explanation

In this example:

  • Constants storeΒ fixed configuration values
  • These values are used throughout the application
  • Since constants cannot be changed, they ensure consistent data

This approach is widely used in:

  • Configuration files
  • Application settings
  • Framework development
  • API configuration

7. Advantages of Using Constants

Using constants in PHP provides several benefits.

1. Data Security

Constants cannot be modified accidentally, which keeps important data safe.

2. Code Readability

Constants make code easier to understand.

Example:


MAX_USERS
SITE_NAME
DATABASE_VERSION

These names clearly describe their purpose.

3. Easy Maintenance

If a constant value needs to be updated, it can be changed in one place.

4. Improved Performance

Since constants cannot change, PHP handles them efficiently.

8. Difference Between Variables and Constants

Feature Variable Constant
Prefix Uses $ No $
Value Change Can change Cannot change
Declaration $var = value const NAME = value
Access $var ClassName::NAME

9. Conclusion

The const keyword in PHP is used to define class constants, which store values that cannot be modified after they are defined.

Constants are very useful for storing fixed information such as configuration values, company names, system settings, and application data.

Unlike variables, constants do not use the $ symbol, and they are accessed using the scope resolution operator (::).

Inside a class, constants are accessed using the self keyword, while outside the class they are accessed using the class name.

Const keyword – Interview Questions

Q 1: What is const keyword in PHP?
Ans: It is used to define class constants.
Q 2: Can const be used outside a class?
Ans: No.
Q 3: Are constants created with const case-sensitive?
Ans: Yes.
Q 4: Can const values be changed?
Ans: No.
Q 5: When is const evaluated?
Ans: At compile time.

Const keyword – Objective Questions (MCQs)

Q1. const is used to declare ______.






Q2. Constants declared with const are ______.






Q3. const constants are defined ______.






Q4. const can be used inside ______.






Q5. Accessing a class constant uses ______.






Related PHP Tutorials