56

Is there a function and/or object and/or extension in PHP that will let you view all the variables defined in the current scope? Something like:

var_export($GLOBALS)

but only showing variables in the current symbol table.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Alana Storm
  • 164,128
  • 91
  • 395
  • 599

2 Answers2

65

get_defined_vars

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

troelskn
  • 115,121
  • 27
  • 131
  • 155
23

get_defined_vars() does exactly what you want.

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

>>> function test($foo) { print_r(get_defined_vars()); }
>>> test('bar');
Array
(
    [foo] => bar
)
Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
  • The >>> ..., is there some command line PHP console I don't know about? – Alana Storm Apr 04 '09 at 21:43
  • 8
    Yes, it's called phpa, you can get it here: http://david.acz.org/phpa/ You could also use the command `php -a` to run PHP interactively, but it's missing some important features. – Paige Ruten Apr 04 '09 at 21:49