PHP Constants

PHP

PHP constants पहचानकर्ता (identifier) एक नाम हैं। वे variable की तरह होते हैं। परन्तु, constant अगर एक एक बार परिभाषित (defined) हो जाते है तो इसे कभी भी changed या undefined नहीं किया जा सकता है। वे पूरे प्रोग्राम में स्थिर (constant) रहते हैं।

Constant और Variables के बीच अंतर

  • Constant नाम से पहले कोई $ sign की आवश्यकता नहीं है।
  • Constant को केवल define() function का उपयोग करके परिभाषित किया जा सकता है।
  • Constant किसी भी variable scope नियमों का पालन नहीं करता है और इसे स्क्रिप्ट में कहीं भी परिभाषित (defined) और access किया जा सकता है।
  • Constants को एक बार निर्धारित कर देने के बाद, इसे परिवर्तित या अपरिभाषित नहीं किया जा सकता है

Basic नियम PHP Constants के लिए

  • एक Constant हमेसा case-sensitive होता है।
  • Constant के नाम के लिए केवल एक ही special character _ का उपयोग किया जा सकता है
  • Constant का नाम किसी numbers से प्रारंभ नहीं हो सकता है।
नोट: Constant के नाम आमतौर पर बड़े अक्षरों में लिखे जाते हैं। ताकि इसे आसानी से पहचान और variable से अलग किया जा सके।
Syntax
define("name", value, case-insensitive)

Constant का नाम case-insensitive के लिए default वैल्यू false होता है, जिसका मतलब है कि यदि आप चाहते हैं कि constant यहाँ पर case sensitive रहें तो खाली छोड़ दें। दूसरी ओर, यदि आप चाहते हैं कि constant case sensitive ना रहे तो आपको सही true लिखना होगा।

Example: Case-sensitive PHP Constants
<?php
define("MESSAGE", "Hello World");
echo MESSAGE;
?>
Example: Not Case-sensitive Constants
<?php
define("MESSAGE", "Hello World", true);
echo MESSAGE, "<br>";
echo message;
?>

Constant vs Variables

Constant Variables
Constants को एक बार निर्धारित कर देने के बाद, इसे changed या undefined नहीं किया जा सकता है। एक variable को असानी से changed या undefined किया जा सकता है।
Constant को केवल define() function का उपयोग करके परिभाषित किया जा सकता है। Variable को एक सरल operator (=) से परिभाषित किया जा सकता है।
Constant नाम से पहले कोई $ sign की आवश्यकता नहीं है। Variable के नाम से पहले हमेसा $ sign की आवश्यकता नहीं है।
Constant किसी भी variable scope नियमों का पालन नहीं करता है और इसे स्क्रिप्ट में कहीं भी परिभाषित (defined) और access किया जा सकता है। Variables को प्रोग्राम में कही भी परिभाषित (defined) किया जा सकता है, परन्तु इन्हें variable scope नियमों का पालन करना पड़ता है।.
Default रूप से, constants हमेसा global रहता है। Variables, local, global या static कुछ भी हो सकता है।
Article By: Brajlal Prasad
Created on: 15 Feb 2023  2641  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]