【发布时间】:2015-02-04 16:29:27
【问题描述】:
似乎我的包含文件的位置会影响包含文件内的全局变量。情况很复杂。见下文:
/config.php
<?php
$domain = 'localhost';
$database = 'db';
?>
/functions.php
<?php
require_once("config.php");
function getDatabase() {
global $database;
return $database;
}
?>
/endpoint.php
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/functions.php");
print(getDatabase());
?>
/api/endpoint.php
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/functions.php");
print(getDatabase());
?>
当我导航到/endpoint.php 时,会打印出db。当我导航到/api/endpoint.php 时,没有打印任何内容。有人可以解释一下这种行为吗?
顺便说一句:我正在使用 XAMPP 5.5.19 和 PHP 5.5
【问题讨论】: