W3docs

get_defined_vars()

The get_defined_vars() function is a built-in function in PHP that returns an associative array containing all the defined variables in the current scope,

Introduction

get_defined_vars() is a built-in PHP function that returns an associative array of all variables that exist in the scope where it is called. The array keys are the variable names (without the leading $) and the values are the variables' current values.

The key idea to remember is scope. A "scope" is the region of code in which a variable is visible. PHP has function scope (variables inside a function) and the global scope (variables at the top level of a script). get_defined_vars() reports only what is visible at the exact point where you call it, which makes it a quick way to snapshot state for debugging without naming each variable by hand.

This page covers the syntax, what the function does and does not include, how its result changes with scope, and the most common real-world uses.

Syntax

get_defined_vars(): array

The function takes no arguments and always returns an array. If no variables are defined in the current scope, it returns an empty array.

Basic example

The simplest case is calling it in the global scope, right after defining a few variables:

<?php
$name = "Ada";
$age = 36;
$colors = ["red", "green"];

print_r(get_defined_vars());
?>

The user-defined variables appear as keys (along with PHP's superglobals, explained below):

Array
(
    [name] => Ada
    [age] => 36
    [colors] => Array
        (
            [0] => red
            [1] => green
        )
    ...
)

Scope matters: local vs. global

This is the part that trips people up. get_defined_vars() does not see across scope boundaries. A function cannot see the script's global variables unless they were imported (for example with the global keyword or passed as arguments), so calling get_defined_vars() inside a function reports only that function's own variables.

php— editable, runs on the server

Inside the function, only the local variable is visible, so the output is just:

Array
(
    [var3] => 1
)

The global $var1 and $var2 are not listed, because they belong to a different scope. (true prints as 1 because PHP renders boolean true that way in print_r.)

What about superglobals?

A common misconception is that get_defined_vars() always returns the superglobals ($_GET, $_POST, $_SERVER, and so on). It does not. Superglobals are returned only when you call the function in the global scope, because that is where they live. Inside a function they are accessible by name but are not part of that local scope, so they do not appear in the result above. The same rule applies to $GLOBALS — it is not included in the returned array.

Common use cases

Check whether a variable is defined

Because the result is a plain array, you can test for a variable's existence with array_key_exists(). This is handy when a variable might only be set on some code paths:

<?php
$config = "loaded";

$vars = get_defined_vars();

var_dump(array_key_exists("config", $vars));   // bool(true)
var_dump(array_key_exists("missing", $vars));  // bool(false)
?>

For checking a single known variable, isset() is usually clearer; get_defined_vars() shines when you want the full picture at once.

Debug the state inside a function

Drop one call near a tricky line to see every local variable and its value, instead of writing a var_dump() for each:

<?php
function calculatePrice($base, $taxRate) {
  $tax = $base * $taxRate;
  $total = $base + $tax;

  print_r(get_defined_vars());
  return $total;
}

calculatePrice(100, 0.2);
?>

The snapshot shows the arguments and every local along the way:

Array
(
    [base] => 100
    [taxRate] => 0.2
    [tax] => 20
    [total] => 120
)
FunctionUse it when you want to…
compact()Build an array from selected variable names.
extract()Do the reverse — turn an array's keys back into variables.
isset()Test whether one specific variable is set and not null.

See also Variable scope for the rules that govern what get_defined_vars() can see, and PHP superglobals for why they appear only in the global scope.

Conclusion

get_defined_vars() gives you a one-line snapshot of every variable visible in the current scope, which makes it a fast debugging aid and a simple way to inspect or test state programmatically. The single rule to keep in mind is scope: inside a function you see only that function's locals (plus anything explicitly imported), while in the global scope you also see PHP's superglobals. Knowing that boundary is what makes the function predictable to use.

Practice

Practice
What does the PHP function get_defined_vars() do?
What does the PHP function get_defined_vars() do?
Was this page helpful?