TExcelTAD
Home › PHP › PHP Variables Scope

PHP Variables Scope

The scope of a variable is the part of the program where it can be used. PHP's rules here surprise people coming from JavaScript or Python: a function cannot see the variables of the code around it unless you explicitly pass them in.

The three scopes

Scope Where the variable is created Visible in
Global Outside any function Code outside functions only
Local Inside a function (including parameters) That function call only
Static Inside a function with static That function, and it keeps its value between calls

Local scope

Variables created inside a function exist only while that function runs. Each call starts fresh:

php · try it yourself
<?php
function makeGreeting() {
    $message = "Good morning";   // local to makeGreeting()
    return $message . "!";
}

echo makeGreeting() . "\n";
echo isset($message) ? "visible outside\n" : "\$message does not exist out here\n";
Good morning!
$message does not exist out here

Global scope

A variable created at the top level of a script is global. But a normal function cannot read it — inside the function, it simply doesn't exist:

php · try it yourself
<?php
$siteName = "Book Corner";

function showTitle() {
    // $siteName is NOT visible here
    return isset($siteName) ? $siteName : "(no site name)";
}

echo showTitle() . "\n";
echo $siteName . "\n";
(no site name)
Book Corner

This isolation is deliberate: a function can only depend on what you give it, so it cannot accidentally change some distant variable.

The right way: parameters and return values

The clean solution is to pass the value in as a parameter and send results back with return:

php · try it yourself
<?php
$siteName = "Book Corner";

function showTitle(string $name): string {
    return "Welcome to $name";
}

echo showTitle($siteName) . "\n";
Welcome to Book Corner

The global keyword and $GLOBALS

If you really need to reach a global variable from inside a function, PHP offers two tools. The global keyword imports a global variable by name, and the $GLOBALS array holds every global variable:

php · try it yourself
<?php
$visits = 0;

function countVisit() {
    global $visits;      // now $visits refers to the global variable
    $visits++;
}

function resetVisits() {
    $GLOBALS['visits'] = 0;
}

countVisit();
countVisit();
countVisit();
echo "Visits: $visits\n";

resetVisits();
echo "After reset: $visits\n";
Visits: 3
After reset: 0

Tip: Treat global as a last resort. Functions that modify globals are hard to test and hard to reason about. Prefer parameters, return values, or (later) object properties.

Static variables

A static variable is local to the function, but it is created only once and keeps its value between calls. It is useful for counters and simple caches:

php · try it yourself
<?php
function nextTicket(): int {
    static $number = 100;   // initialised only on the first call
    $number++;
    return $number;
}

echo "Ticket " . nextTicket() . "\n";
echo "Ticket " . nextTicket() . "\n";
echo "Ticket " . nextTicket() . "\n";
Ticket 101
Ticket 102
Ticket 103

Without static, $number would be reset to 100 on every call and each ticket would be 101.

Closures and use

Anonymous functions also cannot see outer variables by default. They import them with use, while arrow functions (fn) capture outer variables automatically:

php
$rate = 0.2;
$withTax = function ($price) use ($rate) { return $price * (1 + $rate); };
$withTax2 = fn($price) => $price * (1 + $rate);   // $rate captured automatically

You will explore both in the lessons on callback functions and arrow functions.

Test yourself

A variable $x is assigned outside all functions. Can a regular function read $x directly?

  1. Yes, always
  2. No, unless it uses global $x or $GLOBALS['x'] (or receives it as a parameter)
  3. Only if $x is a number
  4. Only in PHP 8

What is special about a static variable inside a function?

  1. It can be read from outside the function
  2. It keeps its value between calls to the function
  3. It cannot be changed
  4. It is shared by all functions

Which kind of function captures outer variables automatically?

  1. A named function
  2. An anonymous function without use
  3. An arrow function fn() =>
  4. None of them