PHP Variable Handling get_defined_vars() Function



The PHP Variable Handling get_defined_vars() function is a built-in method. It can be used to return an array of all defined variables. This function returns a multidimensional array including the complete list of variables, environment, etc.

The purpose of this function is to see all the variables available at a specific point in your code. This function does not take any input or arguments. The function is helpful when you want to debug or check the variables in your code.

Syntax

Here is the syntax of the PHP Variable Handling get_defined_vars() function −

array get_defined_vars( void )

Parameters

This function does not accept any parameter.

Return Value

The get_defined_vars() function returns a multidimensional array containing a list of all defined variables.

PHP Version

First introduced in core PHP 4.0.4, the get_defined_vars() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Variable Handling get_defined_vars() function to get a multidimensional array.

<?php
   // Declare an array and initialize it
   $a = array(0, 1, 2, 3, 4);
   // Use get_defined_vars() function
   $arr = get_defined_vars();

   // print $arr
   print_r($arr);
?>

Output

Here is the outcome of the following code −

Array (
[_GET] => Array ( )
[_POST] => Array ( )
[_COOKIE] => Array ( )
[_FILES] => Array ( )
[a] => Array (
   [0] => 0
   [1] => 1
   [2] => 2
   [3] => 3
   [4] => 4
))

Example 2

This code demonstrates how to use get_defined_vars() function to display each variable in the current scope.

<?php
   // Declare name and age here
   $name = "Amit";
   $age = 25;

   $definedVariables = get_defined_vars();
   print_r($definedVariables);
?> 

Output

This will generate the below output −

Array
(
   [_GET] => Array
      (
      )
   [_POST] => Array
      (
      )
   [_COOKIE] => Array
      (
      )
   [_FILES] => Array
      (
      )
   [argv] => Array
      (
         [0] => index.php
      )
   [argc] => 1
   . 
   . 
   . 

   [name] => Amit
   [age] => 25
)

Example 3

Now the below code checks if certain variables exist in the defined variables array with the help of the get_defined_vars() function.

<?php
   // Declare city and country here
   $city = "Mumbai";
   $country = "Maharashtra";

   $variables = get_defined_vars();

   if (array_key_exists('city', $variables)) {
      echo "The variable 'city' is defined.\n";
   }
   if (array_key_exists('state', $variables)) {
      echo "The variable 'state' is defined.\n";
   } else {
      echo "The variable 'state' is not defined.\n";
   }
?> 

Output

This will create the below output −

The variable 'city' is defined.
The variable 'state' is not defined.

Example 4

In the following example, we are using the get_defined_vars() function works inside a function and display variables scoped within it.

<?php
   function showVars() {
      $animal = "Dog";
      $color = "Brown";

      $vars = get_defined_vars();
      print_r($vars);
   }

   showVars();
?> 

Output

Following is the output of the above code −

Array
(
   [animal] => Dog
   [color] => Brown
)

As you can see this function returns a multidimensional array containing a list of all defined variables (environment, server or user-defined variables) within the scope that get_defined_vars() is called.

php_variable_handling_functions.htm
Advertisements