What is php constants in hindi-php कांस्तान्ट्स क्या है ?

हेल्लो दोस्तों आज के इस पोस्ट में आपको php constants in hindi  के बारे में बताया गया है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

Introduction to PHP Constants

PHP में variables के अलावा आप constants में भी data store कर सकते है। Constants variables की तरह ही होते है लेकिन इनकी value को change नहीं किया जा सकता है।

कई बार आपको ऐसे data को store करने की आवश्यकता होती है जिसे change नहीं किया जा सके। उदाहरण के लिए आप geometric operations perform कर रहे है जिनमें PI की value की आवश्यकता है। PI की value हमेशा 3.14 ही रहती है।

यदि आप variable के द्वारा PI की value store करते है तो यह संभव है की बाद में गलती से आपके द्वारा या किसी operation से इसकी value change हो जाये। यदि ऐसा होता है तो सभी operations wrong results produce करेंगे।

इस situation में PI को constant के रूप में define किया जा सकता है। ऐसा करने से program run होने के दौरान किसी भी प्रकार से PI की value change नहीं की जा सकती है।

Syntax of PHP Constants

PHP में constants define करने के लिए define function का प्रयोग किया जाता है। इसका general syntax निचे दिया जा रहा है।

define(“constant-name”,”constant-value”,”case-sensitive”);

Constant define करने के लिए define function में 3 arguments pass किये जाते है।

  • constant-name – यह constant का नाम होता है। यह नाम program में unique होना चाहिए। 
  • constant-value – यह constant की value होती है। 
  • case-sensitive (optional) – यह Boolean (TRUE, FALSE) value होती है। यह एक optional argument है इसके बिना भी constant create किया जा सकता है। यदि आप चाहते है की constant non case sensitive हो तो आप इसकी value true pass करते है नहीं तो इसकी value by default false रहती है। 

Constants को program में use करते समय आपको एक बात हमेशा ध्यान रखनी चाहिए की constants को dollar ($) के साथ define और प्रयोग नहीं  किया जाता है। 

Example of PHP Constants

PHP में constants के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।

<html>
<head>
<title>PHP Constant Example</title>
</head>
<body>   <?php // Defining constant
define(“PI”,3.14); // Printing constant
echo “Value of PI is : “.PI; ?> </body>
</html>

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

PHP Magic Constants

PHP आपको बहुत सारे predefined constants provide करती है। इन constants को आप किसी भी script में use कर सकते है।

इसके अलावा PHP आपको magic constants भी provide करती है। इन magic constants की value इनकी location के आधार पर change होती है। इनके बारे में निचे बताया जा रहा है।

  • __LINE__ – यह constant file में current line number को बताता है। 
  • __FILE__ – यह constant current file का पूरा path और पूरा नाम बताता है। 
  • __DIR__ – यह constant current file की directory बताता है। 
  • __FUNCTION__ – यह constant current function का नाम बताता है। 
  • __CLASS__ – यह constant current class का नाम बताता है। 
  • __TRAIT__ – यह constant trait का नाम बताता है। 
  • __METHOD__ – यह constant class के method का नाम बताता है। 
  • __NAMESPACE__ – यह constant current namespace का नाम बताता है। 

PHP magic constant का प्रयोग निचे उदाहरण द्वारा समझाया जा रहा है।

<html>
<head>
<title>PHP Magic Constant Example</title>
</head>
<body> <?php// Printing current file path using PHP magic constant.
echo __FILE__;?></body>
</html>

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

reference-https://www.tutorialspoint.com/php/php_constants.htm

निवेदन-अगर आपको यह आर्टिकल(php constants in hindi) अच्छा लगा हो तो आप इस पोस्ट को अपने दोस्तों के साथ जरुर शेयर करे और आपको जिस टॉपिक पर आपको पढना या नोट्स(php constants in hindi) चाहिए तो हमें जरुर कमेंट करे आपका कमेंट्स(php constants in hindi) हमारे लिए बहु मूल्य है धन्यवाद

Leave a Comment