← Back to PHP Course | Chapter 2: Output & Input | Lesson 7 of 8

PHP $GLOBALS

किसी script के top level पर declare किया गया variable आमतौर पर किसी function के अंदर invisible होता है -- PHP functions को default रूप से अपनी private variable scope मिलती है। $GLOBALS एक special superglobal array है जो इसके आसपास एक तरीका देता है: यह हर global-scope variable रखता है, जो कहीं से भी, किसी function के गहरे अंदर सहित, नाम से accessible है।
Syntax
php
$GLOBALS["variable_name"];                 // read a global inside a function
$GLOBALS["variable_name"] = new_value;     // change the global variable

$GLOBALS में क्या होता है

$GLOBALS को PHP automatically global (top-level, किसी function के बाहर) scope में declare किए गए हर variable से populate करता है -- key उस variable का नाम एक string की तरह है, और value वह variable की current value है, जो global variable बदलते ही live update होती है।

उदाहरण: What $GLOBALS Contains

php
<?php
// Declare `$siteName`, set to "Cookies Cursor"
$siteName = "Cookies Cursor";
// Print a human-readable dump of `$GLOBALS['siteName']`
print_r($GLOBALS['siteName']);
?>

किसी Function के अंदर एक Global Variable पढ़ना

एक function आमतौर पर नाम से किसी global variable को सीधे नहीं देख सकता -- किसी function के अंदर $siteName एक पूरी तरह अलग, local $siteName को refer करता है जो शायद exist ही न करे। $GLOBALS["siteName"] function के अंदर से explicitly global scope में पहुँचकर इसके आसपास जाता है।

उदाहरण: Reading a Global Variable Inside a Function

php
<?php
// Declare `$siteName`, set to "Cookies Cursor"
$siteName = "Cookies Cursor";
// Define the function `showSite` with no parameters
function showSite() {
    // Print `$GLOBALS['siteName']` to the output
    echo $GLOBALS['siteName'];
}
// Call `showSite()`
showSite();
?>

$GLOBALS के through एक Global Variable में Write करना

किसी function के अंदर $GLOBALS["varName"] = $newValue assign करना असली global variable को खुद बदल देता है, और वह change function के return होने के बाद पूरी script में हर जगह visible है -- सिर्फ function call तक scoped एक local copy नहीं।

उदाहरण: Writing to a Global Variable via $GLOBALS

php
<?php
// Declare `$counter`, set to `0`
$counter = 0;
// Define the function `increment` with no parameters
function increment() {
    // Set `$GLOBALS['counter']` to `$GLOBALS['counter'] + 1`
    $GLOBALS['counter'] = $GLOBALS['counter'] + 1;
}
// Call `increment()`
increment();
// Print `$counter` to the output
echo $counter;
?>

$GLOBALS बनाम global Keyword

global keyword, जिसे किसी function के अंदर global $varName; की तरह इस्तेमाल किया जाता है, एक specific global variable को उस function की local scope में by reference import करता है -- उस line के बाद, function के अंदर $varName ऐसे behave करता है जैसे यह असली global variable हो। $GLOBALS इसके बजाय local scope बदले बिना array-style access देता है।

उदाहरण: $GLOBALS vs the global Keyword

php
<?php
// Declare `$siteName`, set to "Cookies Cursor"
$siteName = "Cookies Cursor";
// Define the function `withGlobalKeyword` with no parameters
function withGlobalKeyword() {
    global $siteName;
    // Print `$siteName . "\n"` to the output
    echo $siteName . "\n";
}
// Define the function `withGlobalsArray` with no parameters
function withGlobalsArray() {
    // Print `$GLOBALS['siteName']` to the output
    echo $GLOBALS['siteName'];
}
// Call `withGlobalKeyword()`
withGlobalKeyword();
// Call `withGlobalsArray()`
withGlobalsArray();
?>

$GLOBALS पर भरोसा करने से कब बचें

$GLOBALS (या global keyword) पर भारी भरोसा किसी function को उन specific global variables से tightly couple कर देता है जिन्हें यह reference करता है, जो उस function को isolation में test करना या किसी अलग context में reuse करना कहीं ज़्यादा मुश्किल बना देता है।

Values को parameters की तरह pass करना और return के through results वापस लेना लगभग हमेशा एक cleaner design है।

उदाहरण: When to Avoid Relying on $GLOBALS

php
<?php
function addTax($price, $rate) {
    return $price + ($price * $rate);
}
echo addTax(100, 0.08);
// Passing values as parameters is easier to test than reaching into $GLOBALS
?>
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. functions में values को proper parameters की तरह pass करने से बचने के तरीके के रूप में $GLOBALS को overuse करना, जो functions को independently test और reuse करना मुश्किल बना देता है।
  2. यह भूल जाना कि किसी function के अंदर $GLOBALS["x"] modify करना असल में real global variable $x को बदल देता है, कोई copy नहीं -- यह surprising side effects का कारण बन सकता है।
  3. $GLOBALS को global keyword से confuse करना -- global $x; एक specific variable को किसी function की scope में import करता है, जबकि $GLOBALS एक array है जो सबको रखता है।
चैप्टर सारांश
  • $GLOBALS एक superglobal associative array है जहाँ हर key किसी global variable का नाम है और हर value उस variable की current value है।
  • किसी function के अंदर $GLOBALS["varName"] पढ़ना या लिखना असली global variable को पढ़ता या लिखता है, कोई copy नहीं।
  • data को इधर-उधर pass करने के लिए $GLOBALS पर भरोसा करने से function parameters और return values इस्तेमाल करना आमतौर पर एक cleaner design है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.