2010-11-21 68 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"; 
?>