PHP Basics · Beginner Level · Chapter 5

PHP Constants क्या है?
Complete Guide in Hindi

PHP Constants की पूरी जानकारी हिंदी में — define(), const keyword, Magic Constants, Predefined Constants, और Variables vs Constants का फर्क। Real examples के साथ।

📌 define() 🔒 const keyword ✨ Magic Constants ⏱️ 8 min read 📝 1600+ words
defineRuntime constant बनाना
constCompile-time constant
8Magic Constants होती हैं
UPPERConstants UPPERCASE लिखते हैं

📋 इस Article में क्या-क्या है (Table of Contents)

  1. Constants क्या होते हैं?
  2. Variables vs Constants
  3. define() से constant बनाना
  4. const keyword से constant बनाना
  5. define() vs const — फर्क
  6. Constant Naming Rules
  7. Magic Constants (8 types)
  8. Predefined Constants
  9. Quick Reference Table
  10. FAQ और Conclusion
1
Constants क्या होते हैं?

Constant एक ऐसा identifier (नाम) है जिसकी value एक बार set होने के बाद बदली नहीं जा सकती। पूरे program में यह same रहती है। Variable में value बदल सकती है — Constant में नहीं।

Real life example — PI (π) की value हमेशा 3.14159 रहती है, कभी नहीं बदलती। इसे constant बनाया जाता है। इसी तरह website का नाम, database name, maximum file size जैसी values जो कभी नहीं बदलतीं — उन्हें constants में store करते हैं।
Constants को पूरे program में कहीं से भी access किया जा सकता है — functions के अंदर भी, classes के अंदर भी — बिना global keyword के। यह variables से बड़ा फर्क है।
Constants = वो values जो program में fix हैं और कभी नहीं बदलतीं। जैसे PI, site name, max upload size, DB credentials।

2
Variables vs Constants — क्या फर्क है?
FeatureVariableConstant
Declaration$naam = "Rahul";define('NAAM', 'Rahul');
$ signज़रूरी है ($)नहीं होता
Value Changeकभी भी बदल सकते हैंबिल्कुल नहीं बदलती
ScopeLocal/Global (limited)Global — हर जगह available
global keywordFunction में ज़रूरीज़रूरी नहीं
Naming StylecamelCase / snake_caseUPPERCASE (convention)
Use caseChanging dataFixed configuration values
VARIABLE vs CONSTANT — Side by Side
<?php

// Variable — value बदल सकती है
$siteName = "HindiNotes";
$siteName = "कुछ और"; // ✅ allowed

// Constant — value नहीं बदलती
define('SITE_NAME', 'HindiNotes');
// define('SITE_NAME', 'कुछ और'); ❌ Error!

// Function के अंदर — constant को global की ज़रूरत नहीं
function showSite() {
  echo SITE_NAME; // ✅ directly access होता है
  // echo $siteName; ❌ undefined — global keyword चाहिए
}
showSite(); // HindiNotes
?>

3
define() से Constant बनाना
define() PHP का built-in function है जो runtime पर constant create करता है। यह सबसे common और traditional तरीका है।
Syntax: define(name, value, case_insensitive); — तीसरा parameter optional है (PHP 8 में deprecated)।
define() — Detailed Examples
<?php

// Basic define — string value
define('SITE_NAME', 'HindiNotesPoint');
define('SITE_URL', 'https://hindinotespoint.com');

// Integer और Float value
define('MAX_UPLOAD_SIZE', 5242880); // 5MB in bytes
define('PI_VALUE', 3.14159);

// Boolean value
define('DEBUG_MODE', true);
define('MAINTENANCE', false);

// Array constant (PHP 7+)
define('ALLOWED_TYPES', ['jpg', 'png', 'gif']);

// Use करना — $ नहीं लगाते
echo SITE_NAME; // HindiNotesPoint
echo MAX_UPLOAD_SIZE; // 5242880
print_r(ALLOWED_TYPES); // Array ( [0] => jpg ... )

// Condition में use
if (DEBUG_MODE) {
  echo "Debug mode ON है";
}
?>
💡 Real World Use: Config files में constants use होते हैं — database name, password, site URL जैसी sensitive values। एक जगह change करो — पूरे project में update हो जाता है।

4
const Keyword से Constant बनाना
const keyword PHP 5.3+ में आया। यह compile-time पर constant बनाता है। Classes के अंदर constants बनाने के लिए यही use होता है।
const keyword — Examples
<?php

// Global scope में const
const APP_NAME = 'HindiNotesPoint';
const APP_VERSION = '2.0';
const TAX_RATE = 0.18; // 18% GST

echo APP_NAME; // HindiNotesPoint
echo APP_VERSION; // 2.0

// Class के अंदर const — सबसे common use
class MathHelper {
  const PI = 3.14159;
  const E = 2.71828;
  const VERSION = '1.0';

  public function circleArea($r) {
    return self::PI * $r * $r;
  }
}

// Class constant access — ClassName::CONSTANT
echo MathHelper::PI; // 3.14159
$m = new MathHelper();
echo $m->circleArea(5); // 78.53975
?>
Class Constants: Class के अंदर constants self::CONSTANT_NAME से access होते हैं। बाहर से ClassName::CONSTANT_NAME से। यह OOP में बहुत common pattern है।

5
define() vs const — क्या choose करें?

✅ define() use करो जब:

// Runtime पर condition में
if ($env === 'prod') {
  define('DB_HOST', 'prod.db');
} else {
  define('DB_HOST', 'localhost');
}

// Functions के अंदर define
function setup() {
  define('READY', true);
}

✅ const use करो जब:

// Class के अंदर — only const
class Config {
  const VERSION = '1.0';
}

// Simple global constants
const MAX_RETRY = 3;
const TIMEOUT = 30;
Featuredefine()const
Execution timeRuntimeCompile-time
Conditional use✅ हाँ (if/else में)❌ नहीं
Function के अंदर✅ हाँ❌ नहीं
Class के अंदर❌ नहीं✅ हाँ
Array support✅ PHP 7+✅ PHP 5.6+
Expression value✅ हाँLimited
Recommended forConfig files, RuntimeClasses, Simple globals
💡 Simple Rule: Class के बाहर simple constants के लिए const prefer करो — यह cleaner और faster है। Class के अंदर const ही use होता है। Runtime/conditional constants के लिए define() use करो।

6
Constant Naming Rules & Conventions

UPPERCASE लिखो (Convention)

Constants हमेशा capital letters में लिखे जाते हैं — SITE_NAME, MAX_SIZE, DB_HOST। यह globally accepted convention है।

Letter या Underscore से शुरू हो

SITE_NAME ✅   _VERSION ✅   1VERSION ❌ — number से शुरू नहीं हो सकता।

Underscore से words separate करो

MAX_UPLOAD_SIZE, DB_HOST_NAME — multiple words को underscore से जोड़ो। camelCase avoid करो constants में।

!

Case Sensitive होते हैं (by default)

SITE_NAME और site_name दो अलग constants हैं। इसीलिए हमेशा UPPERCASE convention follow करो — confusion नहीं होगा।

__ (double underscore) से शुरू मत करो

PHP के Magic Constants double underscore से शुरू और खत्म होते हैं जैसे __LINE__। अपने constants में यह pattern avoid करो।

✅ अच्छे Constant Names

const SITE_NAME = '...';
const MAX_FILE_SIZE = 5242880;
const DB_HOST = 'localhost';
const APP_VERSION = '2.0.1';
const TAX_RATE = 0.18;

❌ बुरे Constant Names

const siteName = '...'; // lowercase
const maxFileSize = 5242880; // camelCase
const X = 'a'; // meaningless
const __MY_CONST__ = 1; // magic style

7
Magic Constants — PHP के Special Constants

Magic Constants PHP के built-in special constants हैं जो double underscore से शुरू और खत्म होते हैं — जैसे __LINE__। इनकी value उस context के हिसाब से बदलती है जहाँ use किए जाते हैं — इसीलिए "magic" कहते हैं।

__LINE__
Current Line Number

जिस line पर use किया है उसका number देता है। Debugging में useful।

__FILE__
Current File Path

Current file का full path और name देता है।

__DIR__
Current Directory

Current file की directory का path। File include करने में useful।

__FUNCTION__
Function Name

जिस function में use किया उसका नाम देता है।

__CLASS__
Class Name

जिस class में use किया उसका नाम देता है।

__METHOD__
Method Name

Class::method format में current method का नाम।

__NAMESPACE__
Namespace Name

Current namespace का नाम देता है।

__TRAIT__
Trait Name

Trait के अंदर use करने पर trait का नाम देता है।

MAGIC CONSTANTS — Real Examples
<?php

// __LINE__ — current line number
echo "यह line " . __LINE__ . " है"; // यह line 5 है

// __FILE__ — full file path
echo __FILE__; // /var/www/html/index.php

// __DIR__ — directory path (file include में useful)
require_once __DIR__ . '/config/database.php';

// __FUNCTION__ — function के अंदर
function myGreeting() {
  echo "Function नाम: " . __FUNCTION__; // myGreeting
}

// __CLASS__ और __METHOD__ — class के अंदर
class User {
  public function getInfo() {
    echo __CLASS__; // User
    echo __METHOD__; // User::getInfo
  }
}
?>
💡 __DIR__ का सबसे important use: File include करते समय always __DIR__ use करो relative path की जगह। require __DIR__ . '/config.php'; — यह हमेशा correct path देता है चाहे script कहीं से भी call हो।

8
Predefined Constants — PHP के Ready-Made Constants
PHP में बहुत सारे Predefined Constants already defined हैं जो PHP automatically provide करता है। इन्हें use करने के लिए कुछ define नहीं करना पड़ता।
PHP_VERSION
PHP Version

Current PHP version string। जैसे "8.3.1"। Version-specific code के लिए।

PHP_INT_MAX
Max Integer

System की maximum integer value। 64-bit पर 9223372036854775807।

PHP_EOL
End of Line

OS-specific line ending। Windows पर \r\n, Linux पर \n। Cross-platform code के लिए।

PHP_INT_MIN
Min Integer

Minimum integer value। Negative maximum value।

PHP_MAJOR_VER
Major Version

PHP_MAJOR_VERSION — सिर्फ major number। जैसे 8।

TRUE/FALSE/NULL
Core Constants

true, false, null — ये भी PHP constants हैं। Case-insensitive हैं।

PREDEFINED CONSTANTS — Examples
<?php

echo PHP_VERSION; // 8.3.1
echo PHP_INT_MAX; // 9223372036854775807
echo PHP_INT_MIN; // -9223372036854775808
echo PHP_FLOAT_MAX; // 1.7976931348623E+308
echo PHP_EOL; // OS की line ending
echo PHP_MAJOR_VERSION; // 8
echo PHP_OS; // Linux / Windows
echo DIRECTORY_SEPARATOR; // / on Linux, \ on Windows

// Version check करना
if (version_compare(PHP_VERSION, '8.0.0', '>=')) {
  echo "PHP 8+ है — modern features use कर सकते हैं!";
}
?>

9
Quick Reference — Constants एक नज़र में
ConceptSyntax / ExampleNote
define()define('PI', 3.14);Runtime constant — सबसे common
constconst PI = 3.14;Compile-time — class में use
Use करनाecho PI;$ नहीं लगाते constants में
Class constantconst VER = '1.0';Class के अंदर
Class constant accessClassName::CONSTANT:: से access
Check करनाdefined('PI')true/false return करता है
__LINE__echo __LINE__;Current line number
__FILE__echo __FILE__;Full file path
__DIR__require __DIR__.'/x.php'Directory path
__FUNCTION__echo __FUNCTION__;Current function name
__CLASS__echo __CLASS__;Current class name
PHP_VERSIONecho PHP_VERSION;PHP version string
PHP_EOLecho "line" . PHP_EOL;OS line ending

10
अक्सर पूछे जाने वाले सवाल (FAQ)
Q: Constant को बाद में change कर सकते हैं?
नहीं। यही Constants का main feature है। एक बार define() या const से बनाने के बाद value permanently fixed हो जाती है। दोबारा define करने की कोशिश करने पर PHP एक Notice देता है और original value रहती है। यह intentional behavior है — constants immutable होते हैं।
Q: defined() function क्या करता है?
defined('CONSTANT_NAME') check करता है कि कोई constant already define हुआ है या नहीं — true या false return करता है। Config files में यह pattern use होता है: if (!defined('DB_HOST')) { define('DB_HOST', 'localhost'); } — इससे duplicate definition से बचते हैं।
Q: Constants और Variables में performance का फर्क है?
हाँ, constants थोड़े faster होते हैं क्योंकि PHP को उनकी value lookup के लिए variable table search नहीं करनी पड़ती। लेकिन यह difference इतना छोटा है कि performance के लिए constants choose नहीं करते — correctness और code clarity के लिए करते हैं।
Q: PHP 8 में define() के तीसरे parameter का क्या हुआ?
define('NAME', 'value', true) का तीसरा parameter case-insensitive constants के लिए था। PHP 7.3 में deprecated हो गया और PHP 8 में completely remove हो गया। अब constants always case-sensitive होते हैं — इसीलिए UPPERCASE convention follow करना और ज़रूरी हो गया है।
Q: Class constants को interface में भी define कर सकते हैं?
हाँ। PHP में Interface के अंदर constants define हो सकती हैं। जो class उस interface को implement करती है वो उन constants को access कर सकती है। यह configuration values को interface के साथ bundle करने का useful pattern है।
Q: Magic Constants और Regular Constants में क्या फर्क है?
Regular constants एक fixed value रखते हैं जो program में कहीं भी same रहती है। Magic Constants context के हिसाब से बदलते हैं — जैसे __LINE__ हर line पर different value देता है, __FUNCTION__ हर function में अलग value देता है। Magic constants PHP automatically provide करता है, इन्हें define नहीं करना पड़ता।

निष्कर्ष (Conclusion)

PHP Constants वो values हैं जो एक बार set होने के बाद बदलती नहीं। यह code को predictable, maintainable, और bug-free बनाते हैं। Config values, mathematical constants, application settings — इन सबके लिए constants perfect choice हैं।

Constants में $ नहीं लगता — यह variables से सबसे visible फर्क है।

define() runtime constant के लिए, const compile-time और class constants के लिए।

UPPERCASE naming follow करो — यह globally accepted convention है।

Magic Constants context के हिसाब से बदलते हैं — __DIR__ सबसे useful है।

Constants globally accessible हैं — functions में global keyword की ज़रूरत नहीं।

Config files में constants use करो — DB credentials, site URL, API keys के लिए।

🚀 अगला कदम: Constants clear हो गए? अब अगले chapter में PHP Operators सीखें — Arithmetic, Comparison, Logical, Assignment, String Operators सब detail में। PHP में calculations और conditions कैसे work करते हैं।