PHP Constants

Introduction

PHP Constant values should not be changed once defined. In PHP, such values are stored using constants.

Constants are very useful when you want to define values like configuration settings, database credentials, or fixed values such as PI or a website name. Unlike variables, constants cannot be modified once they are defined.

What is a PHP Constant?

A PHP Constant is a name or identifier for a simple value that cannot be changed during the execution of the script.

📖
Key characteristics of constants:
  • They do not use the $ symbol.
  • Their value cannot be changed once defined.
  • They are global and can be accessed anywhere in the script.
  • Usually written in uppercase for readability.

Example:


<?php
define("SITE_NAME", "TechFliez");
echo SITE_NAME;
?>

Output:

TechFliez

Note: Here, SITE_NAME is a constant, and its value cannot be changed later.

Why it is Used

1. Fixed Values

Used to store values that should not change (e.g., company name, API keys).

2. Security

Sensitive data like database credentials can be stored safely.

3. Code Consistency

Using constants avoids repeating the same value multiple times.

4. Global Access

Constants can be accessed anywhere in the script.

5. Readability

Makes code more understandable and organized.

Syntax

1. Using define() Function

The most common way to create a constant is using the define() function.


define("CONSTANT_NAME", "value");

Example:


<?php
define("PI", 3.14);
echo PI;
?>

Output:

3.14

2. Using the const Keyword

You can also define constants using the const keyword.


const SITE = "TechFliez";
echo SITE;

Output:

TechFliez

Difference between define() vs const:

  • define() → Can be used anywhere
  • const → Used inside classes or at the top level

Examples:

Example 1: Basic Constant


<?php
define("GREETING", "Hello Friends");
echo GREETING;
?>

Output:

Hello Friends

Example 2: Multiple Constants


<?php
define("NAME", "John");
define("AGE", 25);
echo NAME;
echo AGE;
?>

Example 3: Using const


<?php
const COMPANY = "TechFliez";
echo COMPANY;
?>

Constant vs Variable

Feature Constant Variable
Symbol No $ Uses $
Value Change Not allowed Allowed
Scope Global Depends on scope
Declaration define() or const $var = value

Real-Life Example

Example 1: Website Configuration


<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "1234");
define("SITE_NAME", "TechFliez");

echo "Welcome to " . SITE_NAME;
?>

In real-world applications:

  • Database credentials are stored as constants
  • Website name remains fixed
  • Used in configuration files

Example 2: Mathematical Constant


<?php
define("PI", 3.14);
$radius = 5;
$area = PI * $radius * $radius;
echo $area;
?>

Note: Used in calculations where values should not change.

Common Mistakes

1. Using $ with Constants

Wrong:


$PI = 3.14;

✔️ Correct:


define("PI", 3.14);

2. Trying to Change Constant Value

Wrong:


define("PI", 3.14);
PI = 3.14159; // Error

3. Not Using Uppercase Names

✔️ Best practice:


define("SITE_NAME", "TechFliez");

4. Using Quotes While Accessing

Wrong:


echo "SITE_NAME";

✔️ Correct:


echo SITE_NAME;

5. Defining Constant Twice

Wrong:


define("PI", 3.14);
define("PI", 3.14159); // Warning

Conclusion

PHP constants are used to store fixed values that should not change during program execution. Unlike variables, constants provide better security, consistency, and readability.

Using constants for configuration settings, fixed values, and repeated data can significantly improve your coding practices.

PHP Constants – Interview Questions

Q 1: What is a constant in PHP?
Ans: A constant is an identifier whose value cannot be changed once it is defined.
Q 2: How do you define a constant in PHP?
Ans: Using the define() function or the const keyword.
Q 3: Are PHP constants case-sensitive?
Ans: By default, constants are case-sensitive.
Q 4: Can constants be defined inside a class?
Ans: Yes, using the const keyword.
Q 5: Can constants be accessed globally?
Ans: Yes, constants are automatically global in scope.

PHP Constants – Objective Questions (MCQs)

Q1. Which function defines a constant in PHP?






Q2. Once a constant is defined, it can be ______.






Q3. Which of the following is correct to define a constant?






Q4. Constants are ______ scoped.






Q5. Constant names in PHP are usually written in ______.






Related PHP Tutorials