← Back to PHP Course | Chapter 5: Functions | Lesson 9 of 10

PHP Scope और Variable Lifetime

Scope इस बारे में है कि किसी variable को कहाँ देखा जा सकता है, उन toys जैसे जो एक room से belong करते हैं और दूसरे से नहीं देखे जा सकते। किसी function के अंदर बनाया गया variable function खत्म होने पर गायब हो जाता है।
Syntax
php
$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
<?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();
?>

Global Scope को समझना

किसी script के top level पर, किसी function के बाहर बनाया गया variable global है — लेकिन वह global scope automatically किसी function के *अंदर* available नहीं होती।

वहाँ इसे पढ़ने या modify करने से पहले आपको function body के अंदर explicitly global $variableName; declare करना होगा।

उदाहरण: Understanding Global Scope

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

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

Static Variables

किसी function के अंदर एक variable को static mark करना इसे उस function की अलग-अलग calls के बीच अपनी value retain करने देता है, हर बार अपनी initial value पर reset होने के बजाय — किसी counter जैसी चीज़ के लिए उपयोगी जिसे याद रखना है कि function अब तक कितनी बार चला है।

उदाहरण: Static Variables

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

Parameter Variable Scope

किसी function के अपने parameters exactly local variables जैसे behave करते हैं: वे जो भी caller ने pass किया वह receive करते हैं, सिर्फ उस single call की अवधि तक exist करते हैं, और function के खत्म होकर return होते ही गायब हो जाते हैं।

उदाहरण: Parameter Variable Scope

php
<?php
function greet($name) {
    echo "Hello, $name";
}
greet("Alice");
// $name does not exist here, outside the function
?>
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. global $x; या $GLOBALS[x] के बिना किसी function के अंदर एक global variable इस्तेमाल करना, जो एक undefined variable warning raise करता है।
  2. static $n = 0; declare करना और हर call पर इसके reset होने की उम्मीद करना, जबकि यह calls के बीच अपनी value रखता है।
  3. global को overuse करना, जो dependencies छुपाता है और functions को test करना मुश्किल बनाता है; values को parameters की तरह pass करें।

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.