简体   繁体   English

PHP包含奇怪的行为

[英]PHP include strange behaviour

I am working on a client site (PHP) and there is a very strange behavior. 我在客户端站点(PHP)上工作,并且有一个非常奇怪的行为。 Using include, include_once, require, or require_once also seems to pull in other content that should not be there at all. 使用include,include_once,require或require_once似乎还会引入其他根本不存在的内容。 Has anyone heard of anything like this? 有人听说过这样的话吗?

Even if the include file is empty or has just some text on it, the behavior persists. 即使包含文件为空或仅包含一些文本,该行为仍然存在。 Could this be some kind of setting / global behavior? 这可能是某种设置/全局行为吗?

Thank you 谢谢

For example: 例如:

<?php require_once('dir/subdir/file.php'); ?>

You should always use absolute paths. 您应该始终使用绝对路径。 It's the only reliable way to reference files. 这是引用文件的唯一可靠方法。

<?php require_once(__DIR__ . '/dir/subdir/file.php'); ?>

Or on PHP 5.2: 或在PHP 5.2上:

<?php require_once(dirname(__FILE__) . '/dir/subdir/file.php'); ?>

The include/require function work by copying the contents of the required file to the placeholder where the function is called. include / require函数通过将所需文件的内容复制到调用该函数的占位符来工作。 If the included file is empty, then empty is what is copied. 如果包含的文件为空,则复制的内容为空。 If you have content included that you dont recognize it can mean that your include path maybe is pointing to somewhere else than what are you thinking, or maybe the included file is including some extra files in his turn. 如果您包含的内容没有被您认出,则可能意味着您的包含路径可能指向了您所想之外的其他地方,或者包含的文件又包含了一些额外的文件。

As global settings there is a global php include path specified in php.ini where people add libraries usually or global scripts. 作为全局设置,在php.ini中指定了一个全局php 包含路径 ,人们通常在其中添加库或全局脚本。 By default it should not contain things that affect your scripts. 默认情况下,它不应包含影响脚本的内容。

If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. 如果在include_path中找不到该文件,则include最终将在失败之前检入调用脚本的自身目录和当前工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM