【问题标题】:PHP: include\require behaviorPHP:包含\要求行为
【发布时间】:2010-11-21 19:29:56
【问题描述】:

每当我们包含\需要一些使用 php 之类的文件时

require ('links.php');include ('links.php');

会发生以下两种情况中的哪一种

    示例

假设有文件file.php 带有以下代码

<?php
echo "my things";
?>
<br />

我们将其包含在

------
------
<?php
echo "about to include file";
include ('file.php')
?>
------
------

场景 1: 将包含文件的代码插入到 parents\container files PHP Code 中,然后处理完整的代码并生成 HTML\result....

意思是,首先把这段代码放在前面

------
------
<?php
echo "about to include file";
<?php
echo "my things";
?>
<br />
?>
------
------

然后处理

场景 2: 首先处理包含的文件,然后插入结果

意思是首先处理包含文件并得到结果

mythings<br/>

然后将其放置在父\容器\包含器代码中,然后该代码将被处理 neaning

------
------
<?php
echo "about to include file";
my things<br />
?>
------
------

现在它将被处理

【问题讨论】:

  • 您的问题到底是什么?有什么问题?
  • p.s 场景 1 可能导致语法错误,场景 2 可能导致变量问题以及许多其他逻辑和条件问题
  • @ sarfraz : 没有问题...只是需要解释一下下面发生的事情

标签: php include require


【解决方案1】:

好吧,仅使用“包含”名称可能并不容易理解...

那么 - 当你这样做时会发生什么

<?php
echo "including now...";
include "myFile.php";
echo "blah";
?>

那么它基本上会变成这样:

<?php
echo "including now...";

?>
CONTENTS OF myFile.php HERE
<?php

echo "blah";
?>

意思是在你的例子中它看起来像这样:

<?php
echo "about to include file";
?>
<?php
echo "my things";
?>
<br />
<?php
?>

【讨论】:

  • 如果这么简单,我就不需要发帖this question。有什么想法吗?
【解决方案2】:

这是场景一。

还要注意require 只会输入一次代码!所以:

<?php
echo "about to include file";
require ('file.php');
require ('file.php');
require ('file.php');
echo "included the file";
?>

将产生:

<?php
echo "about to include file";
?><?php
echo "my things";
?>
<br /><?
echo "included the file";
?>

而:

<?php
echo "about to include file";
include ('file.php');
include ('file.php');
include ('file.php');
echo "included the file";
?>

将产生:

<?php
echo "about to include file";
?><?php
echo "my things";
?>
<br /><?php
echo "my things";
?>
<br /><?php
echo "my things";
?>
<br /><?php
echo "included the file";
?>

【讨论】:

    【解决方案3】:

    这是场景一。 include 是一种在该代码行“注入”代码的简单机制。

    作为一个历史花絮,在 PHP 4.1 之前,includes 曾经被处理,即使语句处于从未执行过的块或条件中。除此之外,PHP 没有任何东西可以接近您的场景 2。

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2011-08-11
      • 2012-02-29
      • 2011-09-18
      • 2015-12-18
      • 2015-07-07
      • 2014-03-12
      • 2014-03-14
      • 2016-08-04
      相关资源
      最近更新 更多