Variable scope
Scope is the answer to one question about a variable in PHP: which lines can see it and which copy of it do they change? A function body is the border that most often decides the answer.
Local scope
Variables created inside a function are local: they exist only while the function runs and stay invisible outside it. An outer variable with the same name is a different variable, so the two never overwrite each other by accident.
<?php $name = "Ada"; function show(): void { $name = "Grace"; echo $name, PHP_EOL; } show(); echo $name, PHP_EOL;
php local.php
Grace
Ada
The inner assignment changed only the copy that lives inside the call, so the outer $name still holds Ada after the function returns.
The global keyword
The global keyword imports an outer variable into the function, so reads and writes touch the shared copy. It works, but it hides the data flow: prefer parameters for input and return for output, and reserve global for small scripts and legacy code.
<?php $total = 0; function addBonus(int $points): void { global $total; $total = $total + $points; } addBonus(5); echo $total, PHP_EOL;
php global.php
5
Without the global line the function would work on a fresh local $total and the echo after the call would still print 0.
Static variables
A static variable inside a function keeps its value between calls. It initializes once and then remembers, which suits counters, caches of expensive results, and one-time setup flags.
<?php function counter(): int { static $n = 0; $n++; return $n; } echo counter(), PHP_EOL; echo counter(), PHP_EOL; echo counter(), PHP_EOL;
php static.php
1
2
3
The initialization runs only on the first call, so each later call adds one to the value the last call left behind instead of starting from zero again.
Pass by value and reference
Scalar arguments pass by value: the function works with a copy, and the caller variable stays unchanged. An & before a parameter passes by reference instead, so assignments inside the function modify the caller variable directly. Use references sparingly and document them, because hidden writes surprise readers.
<?php function bump(int &$n): void { $n++; } function unchanged(int $n): void { $n++; } $x = 1; bump($x); echo $x, PHP_EOL; $y = 1; unchanged($y); echo $y, PHP_EOL;
php params.php
2
1
The reference version changed its caller, and the value version changed only its own copy, so the second echo still prints 1.
Exercises
- Remove the global line from global.php and run it. What does the echo print, and why?
- Call counter() twice, store both values and print their sum. Which values do the two calls return?
- Change the reference parameter of bump() to a value parameter and run params.php. What does the first echo print now?
- Write a function with a static array that collects one value per call, then print the array after three calls.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which variables can a function body see by default?
Answer
Only its own local variables and its parameters
A function starts with an empty local scope, filled by its parameters and its own assignments.
-
What does
global $total;inside a function do?Answer
It makes the function use the outer variable of that name
The keyword binds the name to the global variable, so reads and writes reach the outer copy.
-
What makes a
staticlocal variable different from an ordinary local one?Answer
It keeps its value between calls
The static local is created once and keeps its last value, while an ordinary local starts fresh on every call.
-
What does an
&before a parameter do?Answer
It passes the caller variable by reference, so the function can change it
The ampersand binds the parameter to the caller variable itself, so an assignment in the body changes it.
-
What does this program print?
<?php $x = 1; function f(): void { $x = 2; } f(); echo $x;Answer
1
The assignment inside the function creates a local variable, and the outer $x keeps its value.
-
What does this program print?
<?php $x = 1; function g(): void { global $x; $x = 2; } g(); echo $x;Answer
2
The global keyword binds the inner name to the outer variable, so the assignment changes it.
-
What does this program print?
<?php function counter(): int { static $n = 0; $n++; return $n; } counter(); echo counter();Answer
2
The first call leaves 1 in the static variable, so the second call adds one and returns 2.
-
What does this program print?
<?php function bump(int &$n): void { $n++; } $x = 1; bump($x); echo $x;Answer
2
The reference parameter is the caller variable, so the increment inside the function is visible at the call site.
-
What does this program print?
<?php function bump(int $n): void { $n++; } $x = 1; bump($x); echo $x;Answer
1
A scalar argument arrives as a copy, so the increment happens on that copy and the caller keeps 1.
-
What does this program print?
<?php $name = "Ada"; function show(): void { echo $name; } show();Answer
Warning: Undefined variable $name
The function has no access to the outer name, so PHP warns about an undefined variable and prints nothing.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev