PHP Scope और Variable Lifetime
In this page:
$global_variable = value;
function function_name() {
$local_variable = value; // exists only in this function
global $global_variable; // or $GLOBALS["global_variable"]
}
Local Scope को समझना
किसी function के अंदर बनाया गया variable सिर्फ उस function के खुद के execution के अंदर exist करता है — यह बाहर के code को invisible है, और function को फिर से call करना उस variable के एक fresh, empty version से शुरू होता है, पिछली call से कुछ भी याद रखने के बजाय।
उदाहरण: Understanding Local Scope
<?php
// Define the function `counter` with no parameters
function counter() {
// Declare `$count`, set to `0`
$count = 0;
$count++;
// Print `$count . "\n"` to the output
echo $count . "\n";
}
// Call `counter()`
counter();
// Call `counter()`
counter();
?>
Login to try C/C++/Java/PHP code in the editor
Global Scope को समझना
किसी script के top level पर, किसी function के बाहर बनाया गया variable global है — लेकिन वह global scope automatically किसी function के *अंदर* available नहीं होती।
वहाँ इसे पढ़ने या modify करने से पहले आपको function body के अंदर explicitly global $variableName; declare करना होगा।
उदाहरण: Understanding Global Scope
<?php
// Declare `$total`, set to `100`
$total = 100;
// Define the function `showTotal` with no parameters
function showTotal() {
global $total;
// Print `$total` to the output
echo $total;
}
// Call `showTotal()`
showTotal();
?>
Login to try C/C++/Java/PHP code in the editor
GLOBALS Superglobal Array
PHP हर global variable को $GLOBALS नाम के एक superglobal array में mirror करता है, जो global keyword की ज़रूरत के बिना किसी भी function के अंदर से accessible है — $GLOBALS[count] उसी variable को पढ़ता या लिखता है जो global $count; आपको access देता।
उदाहरण: The GLOBALS Superglobal Array
<?php
// Declare `$count`, set to `5`
$count = 5;
// Define the function `showCount` with no parameters
function showCount() {
// Print `$GLOBALS['count']` to the output
echo $GLOBALS['count'];
}
// Call `showCount()`
showCount();
?>
Login to try C/C++/Java/PHP code in the editor
Static Variables
किसी function के अंदर एक variable को static mark करना इसे उस function की अलग-अलग calls के बीच अपनी value retain करने देता है, हर बार अपनी initial value पर reset होने के बजाय — किसी counter जैसी चीज़ के लिए उपयोगी जिसे याद रखना है कि function अब तक कितनी बार चला है।
उदाहरण: Static Variables
<?php
// Define the function `increment` with no parameters
function increment() {
static $count = 0;
$count++;
// Print `$count . "\n"` to the output
echo $count . "\n";
}
// Call `increment()`
increment();
// Call `increment()`
increment();
// Call `increment()`
increment();
?>
Login to try C/C++/Java/PHP code in the editor
Parameter Variable Scope
किसी function के अपने parameters exactly local variables जैसे behave करते हैं: वे जो भी caller ने pass किया वह receive करते हैं, सिर्फ उस single call की अवधि तक exist करते हैं, और function के खत्म होकर return होते ही गायब हो जाते हैं।
उदाहरण: Parameter Variable Scope
<?php
function greet($name) {
echo "Hello, $name";
}
greet("Alice");
// $name does not exist here, outside the function
?>
Login to try C/C++/Java/PHP code in the editor
global $x;या$GLOBALS[x]के बिना किसी function के अंदर एक global variable इस्तेमाल करना, जो एक undefined variable warning raise करता है।static $n = 0;declare करना और हर call पर इसके reset होने की उम्मीद करना, जबकि यह calls के बीच अपनी value रखता है।globalको overuse करना, जो dependencies छुपाता है और functions को test करना मुश्किल बनाता है; values को parameters की तरह pass करें।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: