我在脚本顶部将安装错误报告报告给
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); //On or Off
该报告正在用换行符记录/保存到我的错误日志文件中,但是在屏幕上显示为1条连续的行而没有换行符。
屏幕输出示例:
Warning: getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48 Warning: Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49 Warning: imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62 Warning: imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64 Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66 Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72
错误日志文件示例:
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72
也可以通过换行符使输出显示到屏幕上吗,以便于阅读?
谢谢。
您可以在代码中使用换行符或HTML以在警告和错误之间放入所需的内容:
$error = "some warning";
echo $error;
echo $error;
将输出:
some warningsome warning
但是,您可以向其添加BR,以在浏览器中正确显示:
$error = "some warning";
echo $error."<br />";
echo $error."<br />";
将在浏览器中输出以下内容:
some warning
some warning
或者,如果您使用终端,或在屏幕上输出文本(而不是HTML),则可以使用非html换行符:
$error = "some warning";
echo $error."\r\n";
echo $error."\r\n";
将换行符插入普通文本。
您可以将它们混合并匹配在一起以满足您的需求。您可以做的另一件事是在错误消息周围使用<pre>
和</pre>
标签,以便当错误消息本身读取时,它们显示在浏览器和/或文本文件中:
$error = "some warning";
echo "<pre>";
echo $error;
echo $error;
echo "</pre>";
编辑:如果生成的警告/错误实际上并不是故意显示的,则还可以修改以下内容:
html_errors = 1
在您的PHP.ini文件中,该文件会将内容输出到nice HTML format。
为了在浏览器中正确显示换行符,我的index.php(PHP7.4)中包含以下内容
ini_set('error_append_string', '</pre>');
ini_set('error_prepend_string', '<pre>');
ini_set('display_errors', 1);