PHP get_defined_vars() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, get_defined_vars() returns an array of all the variables that are defined in the current scope. The keys are the variable names and the values are the variable values.

It is most useful inside a function, where it returns the parameters and local variables. At the top level of a script it also includes superglobals and other built-in variables, so the result is long and changes between environments.

Syntax

Call get_defined_vars() with the values you need. Start with this line.

PHP
$vars = get_defined_vars();
CallWhat it does
get_defined_vars()Returns every variable in the current scope as an associative array.
array_keys(get_defined_vars())Returns just the names of the defined variables.
get_defined_vars()['name']Reads the value of $name by using its name as the key.

The array is a copy, so changing it does not change the real variables. Only variables that already exist when you call the function are included. Variables you create afterwards, even the one that receives the result, are not part of it.

Library declarations
PHP
get_defined_vars(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAn associative array of variable names and values for the current scope. In a function it holds the parameters first, then local variables in the order they were created.

PHP Examples

Example 1 — Listing Local Variables

Inside a function, get_defined_vars() returns only that function's own variables.

PHP
<?php
function showVars() {
    $name = "Asha";
    $age = 25;

    $vars = get_defined_vars();
    echo implode(", ", array_keys($vars)), PHP_EOL;
}

showVars();

How It Works

  1. Two local variables, $name and $age, are created inside the function.
  2. get_defined_vars() collects the variables that exist at that moment into an array.
  3. array_keys() extracts the variable names, and implode() joins them with commas.
  4. $vars is not in the list, because it did not exist yet when the function ran.

Example 2 — Parameters Come First

Function parameters count as defined variables, even the ones that use a default value.

PHP
<?php
function greet($first, $last = "Kumar") {
    $full = $first . " " . $last;
    $vars = get_defined_vars();

    echo implode(", ", array_keys($vars)), PHP_EOL;
    echo count($vars), PHP_EOL;
    echo $vars['full'], PHP_EOL;
}

greet("Asha");

How It Works

  1. $first and $last are parameters, so they appear in the array before the local variable.
  2. $last uses its default value, "Kumar", and is still included.
  3. $full is a local variable created inside the function, so it comes after the parameters.
  4. You can read any value from the result by name, such as $vars['full'].

Example 3 — The Result Is a Snapshot

get_defined_vars() returns a copy. Changing a variable later does not change the saved array.

PHP
<?php
function snapshotDemo() {
    $x = 1;
    $snapshot = get_defined_vars();
    $x = 99;

    echo $snapshot['x'], PHP_EOL;
    echo $x, PHP_EOL;
    echo array_key_exists('snapshot', $snapshot) ? "included" : "not included", PHP_EOL;
}

snapshotDemo();

How It Works

  1. $x is set to 1, then get_defined_vars() copies that state into $snapshot.
  2. $x is changed to 99 afterwards. The copy does not see the change, so $snapshot['x'] is still 1.
  3. The variable $x itself now holds 99.
  4. $snapshot did not exist when the function was called, so it is not a key in its own array.

Frequently Asked Questions

It returns an associative array holding every variable defined in the current scope, with variable names as keys and their values as values.
In the global scope it also includes built-in variables such as $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER and $argv. Use it inside a function to get only your own variables.
Yes. Parameters are normal variables in the function scope, so they are included, followed by the local variables you create.
No. The array is a copy of the values. Changing it has no effect on the real variables. Use extract() if you want to turn an array into variables.
compact() builds an array from only the variable names you choose, such as compact('name', 'age'). get_defined_vars() takes every variable in scope. Prefer compact() when you know which variables you need.

Next: get_resource_id()

get_resource_id() returns the unique integer ID of a resource, such as an open file handle.

get_resource_id() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful