dongmiao4733 2011-02-15 13:24
浏览 66
已采纳

PHP在更多函数之间传递变量

I have about 30 variables that I need to pass to 3 functions e.g.

displayform() - where some form data is pulled out from DB and some needs to be entered into the form.

checkform() - which checks if all data is entered properly.

errors() - this will display errors (if any)

processform()- this process all data and store them to DB

Now I am using GLOBAL $variable; to pass those variables between functions, but than I have to declare each variable as global at the function begin and that results in a big file, so I just want to know is there a way to declare variables as globals (preferably only once) so that all functions can use them ?

  • 写回答

4条回答 默认 最新

  • dtqpw68806 2011-02-15 13:27
    关注

    You can try putting all the variables into an associative array and just passing this array between functions, like:

    $omgArray = array();
    $omgArray['lolVar1'] = lolVar1;
    $omgArray['wowVar3'] = wowVar3;
    
    yeaaaFunction($omgArray);
    
    function yeaaaFunction($omgArray){
        echo $omgArray['lolVar1'] . $omgArray['wowVar3'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试