Scope and Static Variables
Scope decides where a variable can be seen. PHP's rule is simple and strict: a function has its own private set of variables, and it can't see variables from outside unless they're passed in. This lesson covers local and global scope, why globals are best avoided, and static variables that remember their value between calls.
Functions have their own scope
Variables created inside a function are local: they exist only while the function runs. And — unlike many languages — a function can't read variables from the code around it:
<?php $siteName = "CodeShop"; function header1(): string { return "Welcome to $siteName"; } echo header1(), "\n";
Warning: Undefined variable $siteName in main.php on line 7 Welcome to
Inside header1(), $siteName is a brand-new, undefined local variable. This isolation is a good thing: a function's behaviour depends only on what it's given, which makes it predictable and reusable.
The fix: pass it in
<?php $siteName = "CodeShop"; function header1(string $siteName): string { return "Welcome to $siteName"; } echo header1($siteName), "\n";
Welcome to CodeShop
Local variables don't leak out
<?php function calculate(): int { $result = 6 * 7; return $result; } $answer = calculate(); echo $answer, "\n"; var_dump(isset($result));
42 bool(false)
Each call gets fresh local variables, so two calls never interfere with each other.
The global keyword
A function can reach a top-level variable with the global keyword or the $GLOBALS array:
<?php $counter = 0; function increment(): void { global $counter; $counter++; } function reset1(): void { $GLOBALS['counter'] = 0; } increment(); increment(); echo $counter, "\n"; reset1(); echo $counter, "\n";
2 0
Avoid this in real code. Any function anywhere can change a global, so when its value is wrong, you have to read the whole program to find out why — and tests can't run functions in isolation. Pass values in as parameters and return results instead; for shared services like a database connection, pass objects around (lesson 34 shows dependency injection).
Static variables
A static local variable is initialised once and keeps its value between calls to the function, while staying private to it:
<?php function nextTicket(): string { static $number = 1000; $number++; return "T-$number"; } echo nextTicket(), "\n"; echo nextTicket(), "\n"; echo nextTicket(), "\n";
T-1001 T-1002 T-1003
Caching with a static variable
Static variables are handy for remembering expensive results within a single request — memoization:
<?php function fib(int $n): int { static $cache = []; if ($n < 2) { return $n; } return $cache[$n] ??= fib($n - 1) + fib($n - 2); } echo fib(50), "\n"; echo fib(90), "\n";
12586269025 2880067194370816120
Without the cache, fib(50) would make billions of calls. Remember that in a web application, static variables only live for one request — every request starts again from scratch.
Superglobals
A few built-in arrays are available everywhere, in every function, without global. They describe the current request and environment:
| Superglobal | Contains |
|---|---|
$_GET, $_POST | Query string and form data (lesson 46) |
$_COOKIE, $_SESSION | Cookies and session data (lessons 48–49) |
$_FILES | Uploaded files (lesson 51) |
$_SERVER | Request details and server information |
$_ENV | Environment variables (often empty; getenv() is more reliable) |
$GLOBALS | All global variables |
<?php function scriptName(): string { return basename($_SERVER['SCRIPT_NAME']); } echo scriptName(), "\n"; echo PHP_SAPI, "\n";
main.php cli
Even though superglobals are reachable from anywhere, good code reads them in one place — at the "edge" of the application — and passes the values on as parameters.
Closures and scope
Anonymous functions also have their own scope, but they can explicitly capture outside variables with use, and arrow functions capture automatically. That's the subject of the next lesson.
Try it yourself
Write a function that counts how many times it has been called and returns a message with the count.
home viewed 1 time(s) about viewed 1 time(s) home viewed 2 time(s)