2013-03-27 109 views
-1

步驟來重新生成問題:PHP:需要/不需要包括警告

總共有3個文件

include.php包含:

<?php 
    $var1 = 5 + $var1; 
?> 

require.php包含:

<?php 
    $var2 = 5 + $var2; 
?> 

incvsreq.php將包含:

這個腳本的
<?php 
$var1 = 0; // Starting with 0 

include('include.php'); // Includes the file so Adding 5 + 0 

echo $var1.'<br/>'; // Result 5 

include_once('include.php'); // Will not include as it is already included 

echo $var1.'<br/>'; // Display again 5, as it was not included above 

include('include.php'); // This will include again so 5 + 5 

echo $var1; // Result 10 

rename('include.php', '_include.php'); // Rename the file 

include('include.php'); // Warning should occur on this line as the file is not present now, but the script should continue. 

$var2 = 0; // Starting with 0 

require('require.php'); // Includes the file so Adding 5 + 0 

echo $var2.'<br/>'; // Result 5 

require_once('require.php'); // Will not include as it is already included 

echo $var2.'<br/>'; // Display again 5, as it was not included above 

require('require.php'); // This will include again so 5 + 5 

echo $var2; // Result 10 

rename('require.php', '_require.php'); // Rename the file 

require('require.php'); // ERROR should occur on this line as the file is not present now, and the script should not continue. 

echo 'This is not displayed'; // This won't be displayed. 
?> 

輸出是:

5 
5 
10 
Warning: include(include.php) [function.include]: failed to open stream: No such file or directory in C:\Program Files\Ampps\www\include\incvsreq.php on line 23 

Warning: include(include.php) [function.include]: failed to open stream: No such file or directory in C:\Program Files\Ampps\www\include\incvsreq.php on line 23 

Warning: include() [function.include]: Failed opening 'include.php' for inclusion (include_path='.;C:\php\pear') in C:\Program Files\Ampps\www\include\incvsreq.php on line 23 
5 
5 
10 
Warning: require(require.php) [function.require]: failed to open stream: No such file or directory in C:\Program Files\Ampps\www\include\incvsreq.php on line 45 

Warning: require(require.php) [function.require]: failed to open stream: No such file or directory in C:\Program Files\Ampps\www\include\incvsreq.php on line 45 

Fatal error: require() [function.require]: Failed opening required 'require.php' (include_path='.;C:\php\pear') in C:\Program Files\Ampps\www\include\incvsreq.php on line 45 

我明白了致命錯誤,在過去和第三警告從上卻絲毫其他的警告是如何出現?我在這裏有點困惑。爲了更好的理解,我評論了每一行。

+0

可能的重複[什麼時候應該使用require \ _once vs include?](http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include) – zaf 2013-03-27 17:44:24

+0

好吧,它是按照您預測的那樣執行完全相同的問題?爲什麼你需要這樣的事情?至少使用模塊化編程並引入功能。 – Ihsan 2013-03-27 17:46:20

+0

@zaf:我知道require和include之間有什麼區別。以及何時使用它。我只是無法理解這些警告include(include.php)[function.include]:無法打開流:沒有這樣的文件或目錄在C:\ Program Files \ Ampps \ www \ include \ incvsreq.php在線23'。 @Ihsan:我不是這樣編寫腳本的。這些只是測試文件。我試圖理解這些警告(第一,第二,第四和第五)。 – Jigar 2013-03-27 18:13:23

回答

0

understood the Fatal Error in the last and 3rd warning from the top but how did other warnings appear ? I am little confused here.

如果你看看錯誤日誌詳細。正如您所期望的那樣,錯誤消息僅在兩行中生成。第23行和第45行。即使它們顯示了3次,它們也只在您要求的那兩行中生成。該代碼似乎表現得像它應該

+0

同意。但爲什麼3次? – Jigar 2013-03-27 18:21:31

+0

由於php正在檢查不同的路徑,如默認包含路徑等 – 2013-03-27 18:30:26

+0

啊!這就說得通了。非常感謝。 – Jigar 2013-03-29 14:06:21

0

錯誤行不同層級的報告和 得到什麼打印出來,通過使用error_reporting在PHP 級別設置更多信息,檢查PHP文檔控制生成味精... error_reporting

+0

感謝您的回覆。找到了 ! – Jigar 2013-03-29 14:07:30