✍️ PHP Basics
Estimated reading: 3 minutes 42 views

🔒 PHP Constants – Learn define() and const with Examples


🧲 Introduction – Why Use Constants in PHP?

In programming, a constant is a value that never changes during the execution of the script. In PHP, constants are used to store fixed values like database credentials, configuration settings, or status flags that should remain the same throughout your application.

🎯 In this guide, you’ll learn:

  • How to define and use constants in PHP
  • Key differences between constants and variables
  • Best practices for naming and organizing constants
  • Use cases for constants in real-world applications

📘 What Is a Constant?

A constant is an identifier (name) for a simple value that cannot be changed once it’s defined.

✅ Unlike variables:

  • Constants do not use $ before their names
  • Constants are global by default
  • Constants are immutable (can’t be re-assigned)

✍️ Defining a Constant in PHP

Use the define() function:

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

✅ Output: Tech369Hub


🧾 Syntax

define(name, value, case_insensitive = false);
ParameterDescription
nameName of the constant (string)
valueConstant value
case_insensitive(Optional) Defaults to false (PHP 7.3 and below only)

⚠️ Note: The case_insensitive parameter is deprecated since PHP 7.4 and removed in PHP 8.0.


📍 Example: Case-Sensitive Constants

<?php
define("GREETING", "Welcome to PHP!");
echo GREETING; // ✅ Works
echo greeting; // ❌ Error (undefined)
?>

📦 Using const Keyword (Alternative Method)

PHP also allows you to define constants using the const keyword (especially within classes):

<?php
const APP_VERSION = "1.0.0";
echo APP_VERSION;
?>

const works only at compile time and outside of conditional blocks.


🧩 Constants vs Variables

FeatureConstant (define)Variable ($var)
PrefixNo $Uses $
Changeable❌ Immutable✅ Mutable
Global Scope✅ Yes (automatically)❌ No (local unless declared global)
Defined withdefine() / const= assignment
Use inside classesconstUse public $var or static

🛠️ Practical Use Case – Configuration File

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
?>

✅ Constants help maintain configuration values in one place and prevent accidental changes.


🧰 Best Practices for Using Constants

Best PracticeWhy It’s Important
🔠 Use uppercase letters (e.g. SITE_URL)Improves readability and follows conventions
✅ Define constants at the top of filesEasy to locate and manage
❌ Don’t override constantsConstants should be treated as locked values
✅ Use const for class-based constantsBetter for OOP-style programming

📌 Summary – Recap & Next Steps

Constants are a key tool for creating predictable, safe, and secure PHP code. They simplify maintenance and prevent bugs by ensuring critical values never change unexpectedly during execution.

🔍 Key Takeaways:

  • Constants are declared with define() or const
  • They do not start with $ and are immutable
  • Constants are global and accessible throughout your application
  • Best suited for fixed configuration values and identifiers
  • Use const inside classes and define() outside

⚙️ Real-World Relevance:
Constants are critical in config files, environment setup, and when defining system-wide flags that must not change—ensuring code stability and reducing bugs in production.


❓ Frequently Asked Questions (FAQ)

❓ Can I change the value of a PHP constant once defined?
❌ No. Constants are immutable—they cannot be redefined or unset.

❓ What’s the difference between define() and const?
define() is used globally and works at runtime. const is used at compile time and within class definitions.

❓ Are constants case-sensitive?
✅ Yes, unless case_insensitive = true is set (deprecated in PHP 7.4 and removed in PHP 8.0+).

❓ Can I use constants in class methods?
✅ Yes. Use the const keyword within the class and access it via ClassName::CONSTANT_NAME.

❓ Should I use variables or constants for database configuration?
✅ Use constants for values that shouldn’t change at runtime, like database credentials.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🔒 PHP Constants

Or Copy Link

CONTENTS
Scroll to Top