get_defined_vars
Returns an array of all variables defined in the current scope, keyed by variable name.
get_defined_vars(): array- Added in PHP 4.0.4
Return value
arrayA multidimensional array with every variable name in the calling scope as a key and its current value as the value.
get_defined_vars() takes a snapshot of the scope it is called from and hands it back as an array: keys are variable names without the $, values are copies of the current values.
What you get depends entirely on where you call it:
- Inside a function or method — only that call's local variables: the parameters and anything assigned so far in the body.
- Inside a closure — the parameters plus the variables actually imported with
use, which makes it a neat way to prove what a closure captured. - At global scope — every global, which in CLI includes
$argv,$argcand the superglobals, so the output is large.
When to reach for it
Its honest use is debugging: one call dumps the whole local state into a log line or a print_r(), which beats writing out ten var_dump() calls. It also pairs with extract() for template rendering, where you snapshot the caller's variables and re-expand them inside an include.
Do not build application logic on it. It returns copies, not references, so writing into the returned array changes nothing, and it cannot see object properties — inside a method you get locals only, never $this's fields. Reach for compact() when you know the names you want.
Example
When to use it
- Dumping every local variable into a log line when an exception is caught, instead of writing a var_dump per variable
- Proving which outer variables a closure actually captured with use
- Snapshotting a controller's locals to hand to a template that calls extract()
Frequently asked questions
Does get_defined_vars() include $this inside a method?
No. It returns local variables only — parameters and variables assigned in the method body. Use get_object_vars($this) to list the object's properties.
Can I modify variables through the array get_defined_vars() returns?
No. The array holds copies, so assigning into it has no effect on the original variables. Use references or extract() if you need to write back.
Why is get_defined_vars() so large at the top level of a script?
At global scope it includes the superglobals and, on CLI, $argv and $argc. Call it inside a function when you only care about local state.
More examples
The result variable is not in its own snapshot
The output is [title, count] — no 'vars' key. The array is built before the assignment completes, so the receiving variable never appears in its own snapshot. Call it a second time and 'vars' would show up.
<?php
function render(): array {
$title = 'Docs';
$count = 3;
$vars = get_defined_vars();
return array_keys($vars);
}
print_r(render());What a closure really captured
Only n and outer appear. $hidden exists in the enclosing scope but was never listed in the use clause, so it is not part of the closure's scope at all — this is a quick way to debug a closure that sees an unexpected null.
<?php
$outer = 'imported';
$hidden = 'not imported';
$fn = function (int $n) use ($outer) {
print_r(get_defined_vars());
};
$fn(5);Method scope excludes object properties
The result is [rate, sum] only. The private property $items is part of the object, not the local scope, so get_defined_vars() cannot see it — use get_object_vars($this) if you need properties too.
<?php
class Cart {
private int $items = 3;
public function total(float $rate): array {
$sum = 10.0 * $rate;
return array_keys(get_defined_vars());
}
}
print_r((new Cart())->total(1.2));
Discussion