php - Python 的 locals() 的 PHP 等价物是什么?

标签 php python scope

我知道$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 正是您要找的。

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/

相关文章:

php - WordPress 主题消失

php - 无法将此元素宽度设置为 100%

php - 为什么我的 PHP 计算器不工作? (字符串函数..)

python - 使用 selenium 和 python 从 HTML 源页面检索文本

python - 你能解释一下为什么这个正则表达式不起作用吗?

C++传递指针

php - 如何获取站点上加载了 javascript/ajax 的 div 的内容?

python - beautifulsoup 只提取前 10 个元素

javascript - 无法在 angularjs Controller 中分配下拉选择值。

JavaScript 评估替代方案