我知道$GLOBALS
在 PHP 中大致相当于 Python 的 globals()
, 但是否有等同于 locals()
的?
我的 python :
>>> g = 'global'
>>> def test():
... l = 'local'
... print repr(globals());
... print repr(locals());
...
>>>
>>> test()
{'g': 'global', [...other stuff in the global scope...]}
{'l': 'local'}
我的 PHP 端口:
<?php
$g = 'global';
function test(){
$l = 'local';
print_r($GLOBALS);
//...please fill in the dots...:-)
}
test();
?>
Array
(
[g] => global
[...other stuff in the global scope...]
)
最佳答案
get_defined_vars
正是您要找的。p>
function test(){
$a = 'local';
$b = 'another';
print_r(get_defined_vars());
}
test();
#Array
#(
# [a] => local
# [b] => another
#)
关于php - Python 的 locals() 的 PHP 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487713/