【问题标题】:How include in PHP works? [duplicate]PHP 中的 include 是如何工作的? [复制]
【发布时间】:2013-08-20 07:04:35
【问题描述】:

我在一个文件夹中有文件 a.php 和数据文件夹。在数据文件夹中,我创建了两个文件:b.php 和 c.php。

一个.php

<?php
   $a = 1;
   include('data/b.php');
?>

b.php

<?php
 include('data/c.php');
?>

c.php

<?php
 echo $a;
?>

当我运行文件 a.php 时,结果为 1。但我将文件 b.php 的内容更改为 include('c.php'); 文件 a.php 的结果也是 1。

我认为它应该显示错误,因为 a.php 和 c.php 不在同一个文件夹中。

【问题讨论】:

  • 读取php.net/include - 从文件夹结构b.phpc.php 看起来它们在同一个文件夹中,这意味着在b.php 中你应该include 'c.php';
  • OK,但是在 b.php 中包含('data/c.php'),文件 a.php 显示 1。
  • 在官方文档中检查这一行: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. Word and 是有原因的。 )
  • 我建议也检查一下this answer

标签: php


【解决方案1】:

如果您包含一个文件,即使来自子文件夹,主“相对”路径仍然是来自第一个文件的路径。阅读更多关于 include / require 函数的信息 - 它们只是在指定位置注入脚本,不会改变包括相对路径。

【讨论】:

  • 请详细说明!
猜你喜欢
  • 1970-01-01
  • 2012-10-19
  • 2014-10-08
  • 2013-04-06
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2014-11-03
  • 2011-07-20
相关资源
最近更新 更多