【问题标题】:PHP : require/include unnecessary warningPHP:需要/包含不必要的警告
【发布时间】:2013-03-17 22:28:14
【问题描述】:

重新生成问题的步骤:

共有 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

我理解了从顶部开始的最后一个和第三个警告中的致命错误,但其他警告是如何出现的?我在这里有点困惑。为了更好地理解,我对每一行进行了注释。

【问题讨论】:

  • 嗯,它的行为与您预测的完全一样,那么问题是什么?为什么你需要这样的东西?至少使用模块化编程并引入一个函数。
  • @zaf :我知道 require 和 include 有什么区别。以及何时使用它。我只是无法理解那些警告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。 @Ihsan:我不是以这种方式编写脚本。这些只是测试文件。我正在尝试理解这些警告(第 1、第 2、第 4 和第 5 次)。

标签: php include require


【解决方案1】:

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 次,它们也只会在您要求的那两行上生成。这段代码似乎表现得像它应该的那样

【讨论】:

  • 因为 php 正在检查不同的路径,例如默认包含路径等
  • 啊哈!这就说得通了。非常感谢。
【解决方案2】:

错误行在几个不同级别的报告中生成一个 MSG 和 打印出来的内容由 PHP 中设置的 error_reporting 级别控制 有关更多信息,请查看 PHP 文档...error_reporting

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 2020-02-21
    • 2015-06-20
    • 1970-01-01
    • 2018-11-08
    • 2023-03-05
    • 2011-12-15
    • 2017-03-19
    相关资源
    最近更新 更多