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

PHP Scope & Variable Lifetime

Scope is about where a variable can be seen, like toys that belong to one room and cannot be seen from another. A variable made inside a function disappears when the function finishes.
Syntax
php
$global_variable = value;

function function_name() {
    $local_variable = value;     // exists only in this function
    global $global_variable;     // or $GLOBALS["global_variable"]
}

Understanding Local Scope

A variable created inside a function exists only within that function's own execution — it's invisible to code outside, and calling the function again starts with a fresh, empty version of that variable rather than remembering anything from the previous call.

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

Understanding Global Scope

A variable created at the top level of a script, outside any function, is global — but that global scope isn't automatically available *inside* a function.

You have to explicitly declare global $variableName; inside the function body before you can read or modify it there.

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

The GLOBALS Superglobal Array

PHP mirrors every global variable into a superglobal array called $GLOBALS, accessible from inside any function without needing the global keyword — $GLOBALS[count] reads or writes the same variable that global $count; would give you access to.

Example: 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

Marking a variable static inside a function makes it retain its value between separate calls to that function, instead of resetting to its initial value each time — useful for something like a counter that needs to remember how many times the function has run so far.

Example: 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

A function's own parameters behave exactly like local variables: they receive whatever the caller passed in, exist only for the duration of that single call, and vanish the moment the function finishes and returns.

Example: 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. #}
Common Mistakes
  1. Using a global variable inside a function without global $x; or $GLOBALS[x], which raises an undefined variable warning.
  2. Declaring static $n = 0; and expecting it to reset on each call, when it keeps its value between calls.
  3. Overusing global, which hides dependencies and makes functions hard to test; pass values as parameters.

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.