PHP Constants क्या है?
Complete Guide in Hindi
PHP Constants की पूरी जानकारी हिंदी में — define(), const keyword, Magic Constants, Predefined Constants, और Variables vs Constants का फर्क। Real examples के साथ।
📋 इस Article में क्या-क्या है (Table of Contents)
- Constants क्या होते हैं?
- Variables vs Constants
- define() से constant बनाना
- const keyword से constant बनाना
- define() vs const — फर्क
- Constant Naming Rules
- Magic Constants (8 types)
- Predefined Constants
- Quick Reference Table
- FAQ और Conclusion
Constant एक ऐसा identifier (नाम) है जिसकी value एक बार set होने के बाद बदली नहीं जा सकती। पूरे program में यह same रहती है। Variable में value बदल सकती है — Constant में नहीं।
| Feature | Variable | Constant |
|---|---|---|
| Declaration | $naam = "Rahul"; | define('NAAM', 'Rahul'); |
| $ sign | ज़रूरी है ($) | नहीं होता |
| Value Change | कभी भी बदल सकते हैं | बिल्कुल नहीं बदलती |
| Scope | Local/Global (limited) | Global — हर जगह available |
| global keyword | Function में ज़रूरी | ज़रूरी नहीं |
| Naming Style | camelCase / snake_case | UPPERCASE (convention) |
| Use case | Changing data | Fixed configuration values |
// 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
?>
// 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 है";
}
?>
// 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
?>
✅ define() use करो जब:
if ($env === 'prod') {
define('DB_HOST', 'prod.db');
} else {
define('DB_HOST', 'localhost');
}
// Functions के अंदर define
function setup() {
define('READY', true);
}
✅ const use करो जब:
class Config {
const VERSION = '1.0';
}
// Simple global constants
const MAX_RETRY = 3;
const TIMEOUT = 30;
| Feature | define() | const |
|---|---|---|
| Execution time | Runtime | Compile-time |
| Conditional use | ✅ हाँ (if/else में) | ❌ नहीं |
| Function के अंदर | ✅ हाँ | ❌ नहीं |
| Class के अंदर | ❌ नहीं | ✅ हाँ |
| Array support | ✅ PHP 7+ | ✅ PHP 5.6+ |
| Expression value | ✅ हाँ | Limited |
| Recommended for | Config files, Runtime | Classes, Simple globals |
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 MAX_FILE_SIZE = 5242880;
const DB_HOST = 'localhost';
const APP_VERSION = '2.0.1';
const TAX_RATE = 0.18;
❌ बुरे Constant Names
const maxFileSize = 5242880; // camelCase
const X = 'a'; // meaningless
const __MY_CONST__ = 1; // magic style
Magic Constants PHP के built-in special constants हैं जो double underscore से शुरू और खत्म होते हैं — जैसे __LINE__। इनकी value उस context के हिसाब से बदलती है जहाँ use किए जाते हैं — इसीलिए "magic" कहते हैं।
Current Line Number
जिस line पर use किया है उसका number देता है। Debugging में useful।
Current File Path
Current file का full path और name देता है।
Current Directory
Current file की directory का path। File include करने में useful।
Function Name
जिस function में use किया उसका नाम देता है।
Class Name
जिस class में use किया उसका नाम देता है।
Method Name
Class::method format में current method का नाम।
Namespace Name
Current namespace का नाम देता है।
Trait Name
Trait के अंदर use करने पर trait का नाम देता है।
// __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
}
}
?>
PHP Version
Current PHP version string। जैसे "8.3.1"। Version-specific code के लिए।
Max Integer
System की maximum integer value। 64-bit पर 9223372036854775807।
End of Line
OS-specific line ending। Windows पर \r\n, Linux पर \n। Cross-platform code के लिए।
Min Integer
Minimum integer value। Negative maximum value।
Major Version
PHP_MAJOR_VERSION — सिर्फ major number। जैसे 8।
Core Constants
true, false, null — ये भी PHP constants हैं। Case-insensitive हैं।
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 कर सकते हैं!";
}
?>
| Concept | Syntax / Example | Note |
|---|---|---|
| define() | define('PI', 3.14); | Runtime constant — सबसे common |
| const | const PI = 3.14; | Compile-time — class में use |
| Use करना | echo PI; | $ नहीं लगाते constants में |
| Class constant | const VER = '1.0'; | Class के अंदर |
| Class constant access | ClassName::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_VERSION | echo PHP_VERSION; | PHP version string |
| PHP_EOL | echo "line" . PHP_EOL; | OS line ending |
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 के लिए।