PHP Constants
In this page:
define("CONSTANT_NAME", value);
const CONSTANT_NAME = value;
echo CONSTANT_NAME; // no $ sign
define() से Constants Define करना
define(SITE_NAME, 'Cookies & Cursor') runtime पर एक constant बनाता है।
एक बार set हो जाने पर, एक constant की value script के execution के बाकी हिस्से के लिए locked हो जाती है — constants के लिए variables जैसा कोई reassignment operator नहीं है, जो exactly वह guarantee है जो आप mid-script कभी न बदलने वाली values के लिए चाहते हैं।
उदाहरण: Defining Constants with define()
<?php
// Call `define('SITE_NAME', 'Cookies & Cursor')`
define('SITE_NAME', 'Cookies & Cursor');
// Print `SITE_NAME` to the output
echo SITE_NAME;
?>
Login to try C/C++/Java/PHP code in the editor
const Keyword
const keyword runtime के बजाय compile time पर एक constant define करता है, जो इसे थोड़ा तेज़ बनाता है और यह किसी class के अंदर constants declare करने का standard तरीका है।
एक class के बाहर, define() और const दोनों काम करते हैं, लेकिन const को value एक fixed literal चाहिए न कि किसी function call का result।
उदाहरण: The const Keyword
<?php
const MAX_USERS = 100;
// Print `MAX_USERS` to the output
echo MAX_USERS;
?>
Login to try C/C++/Java/PHP code in the editor
Constant Scope
किसी variable के उलट, एक constant define होते ही script में automatically हर जगह accessible हो जाता है — हर function और हर included file के अंदर — बिना global keyword की ज़रूरत के।
यह global-by-default behavior exactly वही वजह है जिससे constants configuration settings या fixed limits जैसी values के लिए suit करते हैं।
उदाहरण: Constant Scope
<?php
// Call `define('APP_VERSION', '1.0')`
define('APP_VERSION', '1.0');
// Define the function `showVersion` with no parameters
function showVersion() {
// Print `APP_VERSION` to the output
echo APP_VERSION;
}
// Call `showVersion()`
showVersion();
?>
Login to try C/C++/Java/PHP code in the editor
Magic Constants
PHP कई 'magic constants' predefine करता है — जैसे __LINE__ और __FILE__ — जिनकी value आपके code में जहाँ भी वे दिखें उस जगह पर depend करते हुए बदलती है।
__LINE__ हमेशा current line number को evaluate होता है, और __FILE__ उस file के full path तक, जो दोनों को logging और debugging के लिए genuinely उपयोगी बनाता है।
उदाहरण: Magic Constants
<?php
// Print "Line: " . __LINE__ . "\n" to the output
echo "Line: " . __LINE__ . "\n";
// Print `"File: " . __FILE__` to the output
echo "File: " . __FILE__;
?>
Login to try C/C++/Java/PHP code in the editor
Constant Best Practices
Constant names को ALL_CAPS में लिखना PHP द्वारा enforce नहीं किया जाता, लेकिन यह एक लगभग universal convention है जो पढ़ने वाले को तुरंत बताता है कि MAX_RETRIES एक fixed constant है, कोई variable नहीं जो बदल सकता है।
Related constants को किसी file के top के पास एक साथ group करना बाद में configuration ढूँढना भी आसान बनाता है।
उदाहरण: Constant Best Practices
<?php
// Call `define('MAX_RETRIES', 3)`
define('MAX_RETRIES', 3);
// Call `define('MIN_RETRIES', 1)`
define('MIN_RETRIES', 1);
// Print `MAX_RETRIES` to the output
echo MAX_RETRIES;
?>
Login to try C/C++/Java/PHP code in the editor
- किसी constant name से पहले
$लिखना (echo $SITE_NAME;) या यह भूल जाना कि constants bare इस्तेमाल होते हैं, ताकि PHP एक ऐसे variable को ढूँढे जो exist ही नहीं करता। SITE_NAME = New;से किसी constant को बदलने की कोशिश करना याdefine()से इसे redefine करना, जो fail हो जाता है या एक warning raise करता है क्योंकि constants बदल नहीं सकते।- किसी
ifया function के अंदरconstइस्तेमाल करना, जिसकी अनुमति सिर्फ top level scope पर है; conditional definitions के लिएdefine()इस्तेमाल करें।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: