How to get all variables defined in the current sc

2019-01-21 19:03发布

问题:

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.

回答1:

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.



回答2:

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
)