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

see*_*ker 0 php

我是php的新手,而且我正在攻击一些moodle代码以获取原型.

Anyhows我有一些数据ID,如从file1.php传递到file2.php,我使用全局变量.这些值在file2中初始化,我需要它们在file1中使用.这是我如何去做

file1.php

 <?php// top of file 1

 global $content; // discussion content // line 3
Run Code Online (Sandbox Code Playgroud)

file2.php

global $content;// line 3379

  $content=$post->subject;
Run Code Online (Sandbox Code Playgroud)

回到file1.php

   echo 'global scope'.$content; // this is always empty// line 168
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?附加文件file1.phpfile2.php

Ben*_*Ben 5

您使用global来获取全局变量

//file 1
$a = "im a global variable";

function foo(){
    global $a;
    echo $a;
}

//file 2
require "file1.php";

function foo2(){
    global $a;
    echo $a;
}
Run Code Online (Sandbox Code Playgroud)