PHP get_defined_vars() Function

What is PHP get_defined_vars() Function?

If you want to get all the defined variables in your script be them environment variables, server variables and user-defined variables within a scope, call get_defined_vars() function You’ll get all the variables that are available in a scope from where the get_defined_vars() function is called. (Check example 1).

Syntax:

get_defined_vars()

Parameters:

The function has no parameter.

Syntax to retrieve a specific variable:

get_defined_vars()[variable_name]

Parameters:

variable_name – It returns all the variable detail of the “variable_name” variable. Check example 5.

Return Values:

The function returns all the defined variables within the scope that get_defined_vars() is called as the form of a multi-dimensional array.

Examples:

Example 1:

<pre>
<?php
$topic = "Web development";
$version = 1.0;
$freeLearning = TRUE;
print_r(get_defined_vars());
?>
</pre>

Output:

Array
(
    [_GET] => Array
        (
        )
    [_POST] => Array
        (
        )
    [_COOKIE] => Array
        (
        )
    [_FILES] => Array
        (
        )
    [topic] => Web development
    [version] => 1
    [freeLearning] => 1
)

Example 2:

<pre>
<?php
$topic = "Web development";
$version = 1.0;
print_r(get_defined_vars());
$freeLearning = TRUE;
?>
</pre>

Output:

Array
(
    [_GET] => Array
        (
        )
    [_POST] => Array
        (
        )
    [_COOKIE] => Array
        (
        )
    [_FILES] => Array
        (
        )
    [topic] => Web development
    [version] => 1
)

Explanation:

As the variable $freeLearning is declared after the get_defined_vars() function, it doesn’t returns $freeLearning variable.

Example 3:

<pre>
<?php
$firstNumber = 10;
function setNumbers(){
    $secondNumber = 20;
    $thirdNumber = 30;
}
$topic = "Web development";
$freeLearning = TRUE;
setNumbers();
print_r(get_defined_vars());
?>
</pre>

Output:

Array
(
    [_GET] => Array
        (
        )
    [_POST] => Array
        (
        )
    [_COOKIE] => Array
        (
        )
    [_FILES] => Array
        (
        )
    [firstNumber] => 10
    [topic] => Web development
    [freeLearning] => 1
)

Explanation:

As the function get_defined_vars() is called in global scope, it only returns variables that are declared in global scope, not the two variables ($secondNumber & $thirdNumber) that are declared in local scope.

Example 4:

<pre>
<?php
$firstNumber = 10;
function setNumbers1(){
    $secondNumber = 20;
    $thirdNumber = 30;
    print_r(get_defined_vars());
}
$topic = "Web development";
$freeLearning = TRUE;
setNumbers1();
?>
</pre>

Output:

Array
(
    [secondNumber] => 20
    [thirdNumber] => 30
)

Explanation:

As the function get_defined_vars() is called in local scope, it only returns variables that are declared in global scope, not the two variables ($topic & $freeLearning) that are declared in local scope.

Example 5:

<pre>
<?php
$topic = "Web development";
$version = 1.0;
$freeLearning = TRUE;
print_r(get_defined_vars()["topic"]);
?>
</pre>

Output:

Web development

Example 6:

<pre>
<?php
$firstNumber = 10;
function setNumbers2(){
    $secondNumber = 20;
    $thirdNumber = 30;
    unset($thirdNumber);
    print_r(get_defined_vars());
}
$topic = "Web development";
$freeLearning = TRUE;
setNumbers2();
?>
</pre>

Output:

Array
(
    [secondNumber] => 20
)

Explanation:

Though two variables ($secondNumber and $thirdNumber) are in local scope of the setNumbers2() function, the $thirdNumber is destroyed by the unset() function. So, the get_defined_vars() returns only $secondNumber variable.

Example 7:

<pre>
<?php
$firstNumber = 10;
function setNumbers3(){
    global $firstNumber;
    $secondNumber = 20;
    $thirdNumber = 30;
    print_r(get_defined_vars());
}
$topic = "Web development";
$freeLearning = TRUE;
setNumbers3();
?>
</pre>

Output:

Array
(
    [firstNumber] => 10
    [secondNumber] => 20
    [thirdNumber] => 30
)

Explanation:

As the variable $firstNumber declared in the global scope is accessed in the local scope inside the setNumbers3() function, the get_defined_vars() function return this variable.

Practical Usages of get_defined_vars() Function:

For debugging purpose, you may use get_defined_vars() function to get the full list of all the defined variables.

Notes on get_defined_vars() function:

  • The function returns variables that has already been declared. It will not return any variable declared later after this function even those are in same scope. Check example 2.
  • Unset variables are not returned by this function. Check example 6.
  • The function can return variables of another scope if you access that variable. Check example 7.

PHP Version Support:

PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8

Summary: PHP get_defined_vars() Function

When you want to list functions in your script for any reason like debugging use get_defined_vars() function. It is one of the built-in variable handling functions in PHP.

Reference:

https://www.php.net/manual/en/function.get-defined-vars.php