0

简介

我正在开发一个非常简单的 php 项目,其中我制作了一个 config.php 文件,其中包含维护网站所需的所有应用程序/配置变量,例如:[database/server/uername/pass etc.]

现在,当我从配置文件中调用变量时,它会引发错误。

问题的产生。

文件层次结构:

 - /..
 - lib     
 :  :
 :  - config.php
 :  - include.php
 :
 - index.php

config.php 代码

<?php
define('database','dbTest');
?>

include.php 的代码

<?php
include('config.php');
echo database; // comment this line when u make index.php
?>

[如果你使用 xampp 运行 include.php,你可能会得到输出,因为dbTest原因很简单,因为我们包含了config.php,我们能够访问数据库变量]

现在的问题是当我们在另一个文件中包含include.php文件时。

index.php 的代码

<?php
include('lib/include.php');
echo database;
?>

现在,当我运行index.php时,出现以下错误。

错误

注意:使用未定义的常量数据库 - 在第 3 行数据库上的 C:\xampp\htdocs..\test\lib\sam.php 中假定为“数据库”

你能指导我如何解决这个问题。

4

1 回答 1

1

将include.php中的包含更改为:

include(__DIR__ . '/config.php');

那是因为相对路径是相对于index.php而不是include.php。但是__DIR__会给你一个绝对路径到存储include.php的文件夹。

于 2013-05-12T13:26:34.090 回答