dpbdl44228 2012-02-16 12:47
浏览 45

如何从前一个范围获取所有声明的变量?

// SCOPE 1
printsmth();    

function printsmth(){
  // SCOPE 2 (printsmth)

  // here (inside SCOPE 2) get variables visible in SCOPE 1
  // var_dump($vars);
}

Can I do this?

Essentially I want to get all declared variables from where my function was called, but inside the function (and ignore variables declared in my function)


something like getting the result of get_defined_vars();, within my function, but for the previous scope :)


ok even more clearly - I just want my own clone of the `get_defined_vars` function. When I call my function it should display a list of declared variables in the current scope.

  • 写回答

5条回答 默认 最新

  • duanqian9593 2012-02-16 12:50
    关注

    Use global keyword:

    $variable = true;
    
    function printsmth(){
      global $variable;
    
      if ( $variable ) {
        echo 'it works';
      }
    }
    
    printsmth();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作