The get_defined_vars Function
The get_defined_vars
function returns an associative array containing all variables
defined in the current scope, including environment variables, superglobal arrays,
and user-defined variables. The function does not accept any parameters.
Syntax
get_defined_vars();
Example
Get all defined variables in the current scope:
<?php
$a = 1;
$b = 'abc';
$res = get_defined_vars();
print_r($res);
?>
Execution result:
[
'a' => 1
'b' => 'abc'
'res' => []
'_GET' => []
'_POST' => []
...
]
Example
Using the function for debugging:
<?php
$x = 10;
$y = 20;
debug_print_backtrace();
var_dump(get_defined_vars());
?>
Execution result:
[
'x' => int(10)
'y' => int(20)
'_GET' => array(0) {}
...
]
See Also
-
the
get_defined_functions
function,
which returns an array of all defined functions -
the
get_defined_constants
function,
which returns an array of all defined constants