PHP में Constants, Identifier होते हैं जिन्हे हम variables की तरह ही define करते हैं , हालाँकि variables की तरह इनकी value mutable नहीं होती , means हम script के execution के समय इनकी value को change नहीं कर सकते हैं।जैसे हम variable को Define करके हम Runtime पर भी increment या decrement करके value change करते हैं।

Understanding Identifier - identifier का use program में किसी entity को Identify करने के लिए किया जाता है, ये unique होते है, define करने के बाद हम इन्हे program में कही भी use कर सकते हैं।

PHP में constants define करने के लिए हम define('var_name ', 'value') function का use करते हैं।

Example -

<?php
define("FOO", "something"); define("FOO2", "something else"); define("FOO_BAR", "something more"); define("__FOO__", "something"); /*Now print These values*/ echo FOO ."<br>"; echo FOO2 ."<br>"; echo FOO_BAR ."<br>"; echo __FOO__ ."<br>"; ?>

Output :

something
something else
something more
something

OK , Example में आप देख सकते हैं कि PHP में constants को कैसे और किन किन तरीकों से define कर सकते हैं।

Note - हालाँकि Class में हम constants define करने के लिए define() function नहीं use कर सकते हैं यह सिर्फ Out Of The Class ही काम करता है।

Class में define करने के लिए हम const keyword का use करते हैं। Classes में variable और Constants variable कैसे define करते हैं यह हम आगे पढ़ेंगे।

Why Use Constants In PHP ?

अब जबकि constant variable को डिफाइन करने के बाद हम इसकी value को change नहीं कर सकते है तो सवाल अत है कि इन्हे हम use क्यों करते हैं , PHP में constant को use करने के कुछ reasons हैं

  • जब हमें यह मालूम हो की program में variable की value change नहीं होगी वहाँ constant use करते हैं , जैसे जब हम PHP में MySQL Connection establish करते हैं तो वहाँ पर use होने वाली value complete Project के लिए Immutable होती हैं।

Note - PHP में Constants Global होते हैं , this means हम function के बाहर Constants को define करके function के अंदर एक्सेस कर सकते हैं। इसके अलावा हम Constants variable में array भी store करा सकते हैं।

Example -

<?php
/*defining constant array variable outside the function*/ define('arr_var', ["leanrhindituts", "learn tutorials in hindi"]); function test_fun() { /*access array variable*/ foreach (arr_var as $value) { echo $value."<br>"; } } /*call the function*/ test_fun(); ?>

Output :

leanrhindituts
learn tutorials in hindi

अब शायद आप constants variables के बारे में clear हो गए होंगे कि किस तरह से constants variable में single value के अलावा array भी स्टोर कराकर use में ले सकते हैं।

इसके अलावा PHP हमें कुछ Magic Constants भी provide करतीं है जिनका result उनके use किये गए स्थान के accordingly change होता रहता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers