步驟來重新生成問題: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
我明白了致命錯誤,在過去和第三警告從上卻絲毫其他的警告是如何出現?我在這裏有點困惑。爲了更好的理解,我評論了每一行。
可能的重複[什麼時候應該使用require \ _once vs include?](http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include) – zaf 2013-03-27 17:44:24
好吧,它是按照您預測的那樣執行完全相同的問題?爲什麼你需要這樣的事情?至少使用模塊化編程並引入功能。 – Ihsan 2013-03-27 17:46:20
@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