PHP $GLOBALS
In this page:
$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
// Declare `$siteName`, set to "Cookies Cursor"
$siteName = "Cookies Cursor";
// Print a human-readable dump of `$GLOBALS['siteName']`
print_r($GLOBALS['siteName']);
?>
Login to try C/C++/Java/PHP code in the editor
किसी 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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
$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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
$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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
$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
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
?>
Login to try C/C++/Java/PHP code in the editor
- functions में values को proper parameters की तरह pass करने से बचने के तरीके के रूप में $GLOBALS को overuse करना, जो functions को independently test और reuse करना मुश्किल बना देता है।
- यह भूल जाना कि किसी function के अंदर $GLOBALS["x"] modify करना असल में real global variable $x को बदल देता है, कोई copy नहीं -- यह surprising side effects का कारण बन सकता है।
- $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: