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
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
$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
$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
$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
globalas 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
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:
$rate = 0.2;
$withTax = function ($price) use ($rate) { return $price * (1 + $rate); };
$withTax2 = fn($price) => $price * (1 + $rate); // $rate captured automaticallyYou 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?
- Yes, always
- No, unless it uses
global $xor$GLOBALS['x'](or receives it as a parameter) - Only if
$xis a number - Only in PHP 8
What is special about a static variable inside a function?
- It can be read from outside the function
- It keeps its value between calls to the function
- It cannot be changed
- It is shared by all functions
Which kind of function captures outer variables automatically?
- A named function
- An anonymous
functionwithoutuse - An arrow function
fn() => - None of them