2010-11-21 59 views
0

每当我们包括\使用PHP像PHP:包括要求行为

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

其中以下两种情况之一发生需要一些文件

        实例

可以说有文件file.php与fol lwing代码

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

,我们有这

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

场景1: 包含文件中的代码插入到父母\容器文件PHP代码,然后将完整代码处理& HTML \结果生成....

意思是,先把这个代码先放

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

然后加工

方案2: 所包含的文件被首先处理,并将结果在

意先插入包含文件将被处理,并将所获得的结果

mythings<br/> 

之后,它被放置在父\ container \ includer代码中,然后代码将被处理为

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

,现在将被处理

+0

你的问题是什么呢?问题是什么? – Sarfraz 2010-11-21 19:33:49

+0

ps SCENARIO 1可能导致语法错误,SCENARIO 2可能导致变量问题以及很多其他逻辑和条件问题 – Moon 2010-11-21 19:34:02

+0

'@ sarfraz:'没有问题......只需要解释下面会发生什么 – Moon 2010-11-21 20:23:56

回答

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 
?> 
+0

如果是这样简单,我不需要发布[这个问题](http://stackoverflow.com/questions/7941373/processing-include-require-directives-in-php)。那里有任何想法? – Peter 2011-10-30 02:56:06

1

这情景之一。 include是一种简单的机制,可以在该行代码中“注入”代码。

作为一个历史珍闻,在PHP 4.1之前,include被用于处理,即使语句处于从未执行的块或条件。除此之外,PHP没有任何会接近您的方案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"; 
?>