Where Can You Read a PHP Variable After a Loop or Function?
Predict which PHP variable reads work after loops, functions and includes, with examples of parameters, global, $GLOBALS, static locals and arrow functions.
A PHP variable assigned inside an executed if statement or foreach loop can be read afterward in the same surrounding scope. A variable created inside a function cannot be read outside that function just because the call has finished. The practical question is whether the code crossed a function boundary, not whether it crossed a pair of braces. PHP's scope manual describes global and function scope.
Trace the boundary, not the braces
In this standalone example, both blocks execute. The assignments in them remain available to the code that follows, while $inside belongs to showLocal():
<?php
$status = 'waiting';
if (true) {
$status = 'ready';
$fromIf = 'condition ran';
}
foreach (['Ada', 'Bo'] as $name) {
$fromLoop = $name;
}
function showLocal(): void
{
$inside = 'function only';
echo $inside, "\n";
}
echo $status, "\n";
echo $fromIf, "\n";
echo $fromLoop, "\n";
showLocal();
// $inside is not available for a read here.
Expected output:
ready
condition ran
Bo
function only
The last line comes from inside the function. It does not show that the caller can read $inside. Likewise, the loop's result depends on its assignments actually running: this example has two items, so $fromLoop receives the second one. The manual's user-contributed scope note specifically contrasts PHP blocks with languages where an if or loop introduces a separate variable scope.
Give a function the value it needs
A function does not automatically read an ordinary global variable with the same name. These four functions show different ways a read can succeed. Each declaration and call is included so the output can be predicted independently of the previous example:
<?php
$status = 'ready';
function fromParameter(string $status): string
{
return $status;
}
function fromLocal(): string
{
$status = 'local';
return $status;
}
function fromGlobal(): string
{
global $status;
return $status;
}
function fromGlobalsArray(): string
{
return $GLOBALS['status'];
}
echo fromParameter($status), "\n";
echo fromLocal(), "\n";
echo fromGlobal(), "\n";
echo fromGlobalsArray(), "\n";
echo $status, "\n";
Expected output:
ready
local
ready
ready
ready
The parameter gets its value from the call. fromLocal() instead creates a same-named variable in its own scope; it does not replace the outer $status. Choose a parameter when the caller should supply the value explicitly. Use global $status when the function deliberately needs the same-named global. The PHP manual says global creates a local reference to that global. $GLOBALS['status'] accesses it through the name-keyed array instead. Unlike an ordinary global variable, $GLOBALS is itself a superglobal, so the function can access it without a global declaration.
Keep a value between calls without making it global
A static local retains its value across calls but remains local to its function. Here an outer variable happens to share its name; the two counters are not the same variable:
<?php
$counter = 99;
function nextCount(): int
{
static $counter = 0;
return ++$counter;
}
echo nextCount(), "\n";
echo nextCount(), "\n";
echo $counter, "\n";
Expected output:
1
2
99
This is useful when the function, rather than its caller, should own the value between calls. It does not make the function's $counter available to code outside the function.
Read a parent-scope value in an arrow function
An arrow function binds a variable from its parent scope for use in its body. This is an illustrative snippet with predicted output, not a reported test result:
<?php
$prefix = 'Order';
$arrow = fn($id) => $prefix . ' ' . $id;
echo $arrow(8), "\n";
Expected output:
Order 8
The manual's scope description distinguishes variables created inside anonymous functions, which are confined to those functions, from arrow functions that bind parent-scope variables. When diagnosing an arrow function's read, check which scope supplied the value.
Check the scope at an include
An included file inherits the variable scope of the include line, according to the PHP manual. The following two files place that line inside a function. They are shown with PHP opening tags because each is a separate file.
main.php:
<?php
$message = 'outside function';
function showIncluded(): void
{
$message = 'inside function';
include __DIR__ . '/scope-part.php';
echo $reply, "\n";
}
showIncluded();
echo $message, "\n";
// $reply is not available for a read here.
scope-part.php:
<?php
echo $message, "\n";
$reply = 'set by included file';
Expected output when main.php includes the shown scope-part.php:
inside function
set by included file
outside function
The included code reads the function's $message and sets $reply in that inherited function scope. The caller outside the function still has its own $message; it does not gain access to $reply. When a read fails in a multi-file script, find the include line before assuming the file itself has global scope.
