dongshu4221 2013-11-27 18:32
浏览 20
已采纳

我正确使用这些全局变量吗?

I'm a newbie to php, and I'm in the process of hacking through some moodle code for a prototype.

Anyhows I have some data Id like passed from file1.php to file2.php, for which Im using global variables. The values are intialized in file2 and I need them for use in file1. Here's how I go about it

file1.php

 <?php// top of file 1

 global $content; // discussion content // line 3

file2.php

global $content;// line 3379

  $content=$post->subject;

Back in file1.php

   echo 'global scope'.$content; // this is always empty// line 168

Am I missing something here? Attached files file1.php and file2.php

  • 写回答

2条回答 默认 最新

  • duanfengshang1088 2013-11-27 18:37
    关注

    You use global to get a global variable

    //file 1
    $a = "im a global variable";
    
    function foo(){
        global $a;
        echo $a;
    }
    
    //file 2
    require "file1.php";
    
    function foo2(){
        global $a;
        echo $a;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改