关于php:如何获取当前范围/符号表中定义的所有变量?

How to get all variables defined in the current scope/symbol table?

PHP中是否有一个函数和/或对象和/或扩展可以让您查看当前作用域中定义的所有变量?比如:

1
var_export($GLOBALS)

但只显示当前符号表中的变量。


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.


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.

1
2
3
4
5
6
>>> function test($foo) { print_r(get_defined_vars()); }
>>> test('bar');
Array
(
    [foo] => bar
)