Php Constants Best Tutorial In Hindi 2023

PHP Constants Name या Identifiers होते हैं जिन्हें Script के Execution के दौरान बदला नहीं जा सकता है सिवाय Magic Constants के, जो वास्तव में Constants नहीं हैं। PHP Constants को 2 तरीकों से परिभाषित किया जा सकता है:

  1. Using define() function
  2. Using const keyword

Constants Variable के समान होते हैं, सिवाय एक बार जब वे परिभाषित हो जाते हैं, तो वे कभी भी अपरिभाषित या परिवर्तित नहीं हो सकते। वे पूरे कार्यक्रम में स्थिर रहते हैं। PHP स्थिरांक समान PHP चर नियमों का पालन करते हैं। उदाहरण के लिए, इसे केवल एक अक्षर या अंडरस्कोर से शुरू किया जा सकता है।

परंपरागत रूप से, PHP स्थिरांक को अपरकेस अक्षरों में परिभाषित किया जाना चाहिए।

Variables के विपरीत, Constants पूरी स्क्रिप्ट में स्वचालित रूप से Global होते हैं।

PHP constants: define()

Constant बनाने के लिए define() फ़ंक्शन का उपयोग करें। यह रन टाइम पर constant को परिभाषित करता है। आइए PHP में define() फ़ंक्शन का सिंटैक्स देखें।

define(name, value, case-insensitive);

name: यह Constant का नाम निर्दिष्ट करता है।

value: यह Constant Value निर्दिष्ट करता है।

case-insensitive: निर्दिष्ट करता है कि क्या PHP constants Case Sensitive है। डिफ़ॉल्ट मान False है। इसका मतलब है कि यह डिफ़ॉल्ट रूप से केस सेंसिटिव है।

आइए define() का उपयोग करके PHP स्थिरांक को परिभाषित करने का उदाहरण देखें।

<?php  
  define("MESSAGE", "Hello Hindi Tutorials!");  
  echo MESSAGE;  
?>  
PHP

Output:

Hello Hindi Tutorials!

Case-Insensitive नाम के साथ एक Constant बनाएँ:

<?php  
  define("MESSAGE", "Hello Hindi Tutorials!", true); // case-insensitive  
  echo MESSAGE;
  echo message;
?>
PHP

Output:

Hello Hindi Tutorials!

Hello Hindi Tutorials!

<?php  
  define("MESSAGE", "Hello Hindi Tutorials!", false); // case-insensitive  
  echo MESSAGE;
  echo message;
?>
PHP

Output:

Hello Hindi Tutorials!

Notice: Use of undefined constant message – assumed ‘message’ in C:\xampp\constant.php on line 4 message

PHP constants: const keyword

PHP ने कॉन्स्टेंट बनाने के लिए एक कीवर्ड const पेश किया हैं। const कीवर्ड Compile time पर constant को define करता है। यह एक language construct है, function नहीं। कॉन्स्टेंट कीवर्ड का उपयोग करके define किये गए constant case-sensitive होते हैं।

<?php  
  const MESSAGE="Constant by Hindi Tutorials";  
  echo MESSAGE;  
?>
PHP

Output:

Constant by Hindi Tutorials

Constant() Function:

इको स्टेटमेंट का उपयोग करने के बजाय PHP constants() फ़ंक्शन का उपयोग करके स्थिरांक के मान को प्रिंट करने का एक और तरीका है।

<?php      
    define("MESG", "Hindi Tutorials!");  
    echo MSEG, "</br>";  
    echo constant("MESG"); /both are similar  
?> 
PHP

Output:

Hindi Tutorials!

Hindi Tutorials!

Constant vs Variables

ConstantsVariables
एक बार PHP constants परिभाषित हो जाने के बाद, इसे फिर से परिभाषित नहीं किया जा सकता है।एक चर को Undefinedऔर साथ ही आसानी से Redefined किया जा सकता है।
एक Constant को केवल define() फ़ंक्शन का उपयोग करके परिभाषित किया जा सकता है। इसे किसी साधारण असाइनमेंट (=) द्वारा परिभाषित नहीं किया जा सकता है।एक Variable को असाइनमेंट (=) ऑपरेटर द्वारा परिभाषित किया जा सकता है।
असाइनमेंट के दौरान Constant से पहले डॉलर ($) चिह्न का उपयोग करने की कोई आवश्यकता नहीं है।एक Variable घोषित करने के लिए, हमेशा Variable के पहले डॉलर ($) चिह्न का उपयोग करें।
Constant किसी Variable Scope के नियमों का पालन नहीं करते हैं, और उन्हें कहीं भी परिभाषित और एक्सेस किया जा सकता है।Program में कहीं भी Variable घोषित किए जा सकते हैं, लेकिन वे Variable Scope के नियमों का पालन करते हैं।
Constant वे variables होते हैं जिनके मान पूरे program में नहीं बदले जा सकते।Variable का मान बदला जा सकता है।
डिफ़ॉल्ट रूप से, Constants Global होते हैं।Variables Local, Global या Static हो सकते हैं।
error: Content is protected!! You are not allowed to copy.