dqjo4340 2015-06-12 01:17
浏览 17
已采纳

在函数之外指定全局变量

I have a pretty long extensive function, and the only thing that would change for me to use it elsewhere in the application would be to change the global variable declared inside of it.

function some_function() {
  global $sys;

  // ... Do stuff

}

some_function();

I need to change global $sys; to global $lang; or possibly a few other things, but everything else would stay the same. I was thinking something like:

function some_function($global_var) {
  global $global_var;

  // ... Do stuff

}

some_function($sys);

... or maybe ...

some_function($lang);

How can this be done?

  • 写回答

2条回答 默认 最新

  • dongzhang1875 2015-06-12 01:40
    关注

    Instead of using global, you could just pass the "global var" as a parameter to the function by reference:

    function some_function(&$global_var) {
    
        // ... Do stuff
    
    }
    

    Then you could use this function elsewhere without requiring to change the "global var" name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题