PHP无法从包含的文件中访问变量

问题描述 投票:0回答:1

我有一个config.php文件,其中包含对象$site。我有另一个包含config.php的php文件,并尝试访问$site,但它声称未定义变量。由于它不在函数或类中,因此不能正常工作吗?

config.php

$site = (object) [
    "env" => "development",
    "uri" => $_SERVER["REQUEST_URI"],
    "legal_exempt" => [
        "/intermedial",
        "/document/terms-of-use",
        "/document/nda",
        "/document/privacy-policy",
        "/document/disclaimer"
    ],
    "legal_cookie" => "legal_agree",
    "title_prefix" => "–",
    "legal_db" => $legal_db,
    "product_db" => $product_db,
    "category_db" => $category_db
];

otherfile.php

require_once "config.php";

exit(print_r($site));

错误是典型的:

Notice: Undefined variable: site

我什至已经检查了otherpage.php文件中包含的文件,它显示config.php已加载。

php file scope undefined require
1个回答
0
投票

使用包含功能将文件添加到新文件。

include 'config.php';
var_dump( $site);

通过使用包含,您将能够从另一个文件访问变量。

© www.soinside.com 2019 - 2024. All rights reserved.