PHP:include \ require行为

时间:2010-11-21 19:29:57

标签: php include require

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

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

以下两种情况之一发生

示例

假设文件file.php包含以下代码

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

我们将此包含在

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

情景1: 将包含的文件的代码插入到父代\容器文件PHP代码中,然后处理完整的代码并进行处理。 HTML \ result生成....

意思是,首先将此代码放在首位,如

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

然后处理

情景2: 首先处理包含的文件,并将结果插入

意味着首先处理包含文件并获得结果

mythings<br/>

然后将它放在parent \ container \ includer代码中,然后代码将被处理为neaning

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

现在它将被处理

3 个答案:

答案 0 :(得分:4)

嗯,这可能不是最容易理解的只有“包含”名称......

所以 - 当你做

时会发生什么
<?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
?>

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

是场景一。

另请注意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";
?>