2013-03-27 80 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