php 如何获取当前范围/符号表中定义的所有变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/717852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all variables defined in the current scope/symbol table?
提问by Alan Storm
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:
PHP 中是否有函数和/或对象和/或扩展可以让您查看当前作用域中定义的所有变量?就像是:
var_export($GLOBALS)
but only showing variables in the current symbol table.
但只显示当前符号表中的变量。
回答by troelskn
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()调用范围内所有已定义变量的列表,这些变量可以是环境、服务器或用户定义的变量。
回答by Jeremy Ruten
get_defined_vars()does exactly what you want.
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()范围内的所有已定义变量的列表,这些变量可以是环境、服务器或用户定义的变量。
>>> function test($foo) { print_r(get_defined_vars()); }
>>> test('bar');
Array
(
[foo] => bar
)

