PHP 包含的脚本就像在调用者脚本的目录中一样执行

PHP included script execute as if it was in caller script's directory

我有一个网站,其中有一个树如下:

public_html/
    index.php
    lang.fr.php
    lang.en.php
    lang.php
    description/
        index.php
        lang.fr.php
        lang.en.php
 ... (lots of directory basically like description)

所有 index.php 包括 lang.php,这应该包括每个页面的 lang.fr.phplang.en.php 版本。但是,如果我简单地输入

include("lang.{$lang}.php");

它包括 lang.php 目录的 lang.fr.php

是否可以或是否必须将 lang.php 的内容放在每个页面中?

我认为问题在于 include() 只是在做它应该做的事情,也就是包含它配置的文件 include_path。

您实际上可以更新 PHP 配置文件中的包含路径,或者更好的是,仅在脚本运行期间更新 include_path,如果您愿意,使用 set_ include_path() 函数。最简单的解决方案就是在 include() 中键入整个路径。

这是一些文档:

http://php.net/manual/en/function.set-include-path.php

在包含之前将__DIR__分配给一个变量,并使用该变量来包含适当的语言文件