← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 11 of 13

PHP Constants

एक constant एक permanent marker से लिखे गए name जैसा है: एक बार आप decide कर लें कि यह क्या represent करता है, इसे कभी बदला नहीं जा सकता। यह उन चीज़ों के लिए perfect है जिन्हें same रहना चाहिए, जैसे आपकी website का name।
Syntax
php
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
<?php
// Call `define('SITE_NAME', 'Cookies & Cursor')`
define('SITE_NAME', 'Cookies & Cursor');
// Print `SITE_NAME` to the output
echo SITE_NAME;
?>

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
<?php
const MAX_USERS = 100;
// Print `MAX_USERS` to the output
echo MAX_USERS;
?>

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
<?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();
?>

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
<?php
// Print "Line: " . __LINE__ . "\n" to the output
echo "Line: " . __LINE__ . "\n";
// Print `"File: " . __FILE__` to the output
echo "File: " . __FILE__;
?>

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
<?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;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी constant name से पहले $ लिखना (echo $SITE_NAME;) या यह भूल जाना कि constants bare इस्तेमाल होते हैं, ताकि PHP एक ऐसे variable को ढूँढे जो exist ही नहीं करता।
  2. SITE_NAME = New; से किसी constant को बदलने की कोशिश करना या define() से इसे redefine करना, जो fail हो जाता है या एक warning raise करता है क्योंकि constants बदल नहीं सकते।
  3. किसी if या function के अंदर const इस्तेमाल करना, जिसकी अनुमति सिर्फ top level scope पर है; conditional definitions के लिए define() इस्तेमाल करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.