Scope
A coffee machine is a closed box. From the outside you cannot see what goes on inside it. The machine itself does not look into your pockets either: it works only with what you dropped into the slot.
A function in PHP works the same way. Let's see what happens if a function tries to take a variable from the file:
<?php
$discount = 5;
function finalPrice($price)
{
return $price - $discount; // ✗
}
echo finalPrice(100), "\n";Warning: Undefined variable $discountInside finalPrice there is no $discount, although the file sets it higher up. This is a warning, not a stop: PHP puts null in its place, in arithmetic null counts as zero, and the program prints 100. The discount got lost without a word.
Each function has its own scope
Scope is the set of variables a piece of code can see. The file has its own scope, and each function has its own. Inside a function you see only its parameters and the variables it created itself. Such variables are called local.
The most surprising part is matching names. A variable with the same name inside a function is a different variable, and writing to it does not touch the file's variable.
Press "Next step" and watch the two $total variables: one lives in the {main} frame, the other in the add frame.
1<?php2 3$total = 10;4 5function add(int $x): int6{7 $total = $x + 1;8 return $total;9}10 11$result = add(5);12echo $total, ' ', $result, "\n";
The file gets the variable $total with the value 10.
The program printed 10 6. Writing $total = $x + 1 inside add created a new local variable, and the file's $total stayed 10. When the call ended, the function's variables disappeared together with its frame.
Interviewers often ask this: why can a function not see a variable declared outside it? The answer: a function body has its own scope. In some languages, JavaScript for example, a function sees the variables around it, but in PHP it does not.
How to get a value inside
A parameter
A value from outside is passed in as a parameter, so the function's declaration shows what it needs:
<?php
$discount = 5;
function finalPrice($price, $discount)
{
return $price - $discount;
}
echo finalPrice(100, $discount), "\n"; // 95Inside the function, $discount is a parameter, not the file's variable. The names match, but only the call ties them: finalPrice(100, $discount) copies the value 5 into the parameter.
global
You can write global $discount; at the start of the body, and then the name $discount inside the function is tied to the file's variable. This is rare: the function's declaration does not show that it depends on a variable outside, and any function can change that variable.
Of all variables, only the superglobal arrays are visible inside a function without passing, for example $_GET. PHP creates them itself, and they are available anywhere in the program. Constants made with const and define are visible everywhere too.
static: memory between calls
Normally the machine remembers nothing about the previous customer: every call starts with a clean frame. The exception is a variable with the word static, the machine's own counter.
<?php
function nextTicket()
{
static $number = 100;
$number++;
return 'A' . $number;
}
echo nextTicket(), ' ', nextTicket(), "\n"; // A101 A102
echo nextTicket(), "\n"; // A103Let's go step by step:
static $number = 100;runs once, on the first call.- Each call increases
$numberand returns the ticket number. - The next call sees not
100but what was left from the last time.
From outside, $number is still invisible: static extends the variable's life but does not open it to the file.
Common mistake
People expect a variable created inside a function to be visible after the call:
<?php
function setup()
{
$limit = 10;
}
setup();
echo $limit, "\n"; // ✗Warning: Undefined variable $limitThe variable $limit lived in the frame of the setup() call and disappeared with it. Outside, PHP looks for $limit among the file's variables, does not find it and prints an empty line. The fix is to hand the value back with return 10; and keep it: $limit = setup();.
Key points
- Each function has its own scope: inside it you see only the parameters and local variables.
- A file's variable is not visible inside a function: reading it gives a
Warningandnull, and the same name inside is a different variable. - A value is passed in as a parameter;
globalties a name to the file's variable but hides the dependency. - A
staticvariable gets its starting value once and keeps its value between calls.