步骤来重新生成问题: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