首页 > 解决方案 > 另一个文件中的 PHP 超全局变量

问题描述

我想在文件 file_one 中声明一个变量(php 代码)并在 file_two 中使用它。

根据其文档,可以从任何地方访问超级博客。但在我的情况下可能不是?我在第一个文件中声明了 $a 但我无法在第二个文件中使用它。虽然我可以在第一个文件中的函数中使用它。

$GLOBALS['a']= 3;

function test() {
  echo $GLOBALS['a'];
} 

test();

这按预期工作。但:

文件一:

    $GLOBALS['a']= 3;

文件二:

    echo $GLOBALS['a'];

导致通知:未定义索引:a

Include 'file_one.php" 不是我想使用的东西,因为 file_one 做了很多事情。

标签: phpsuperglobals

解决方案


Store the value of global in a session variable. It will work. Or you can define it in your head/header/function or in any common file.


推荐阅读