自定义错误消息

要触发自定义警告错误,应使用 E_USER_WARNING 类型:

trigger_error("This situation was not expected, but assuming the developer knows about it, we continue!", E_USER_WARNING);

只有 E_USER* 类型应该与 trigger_error 一起使用。

PHP E_WARNING

E_WARNING 错误并不总是很严重,通常我们可以放心地忽略它。
但是,最好在警告发生之前尽量避免警告。

当包含不存在的文件时,以及在发送 HTTP 正文输出后发送 HTTP 标头时,可能会触发警告。
这个错误还有其他原因,而且大多数情况下,它只是因为开发人员忽略了一些东西而发生。

要仅显示 E_WARNING 错误,我们可以使用 error_reporting 函数:

error_reporting(E_WARNING);

要显示所有错误,请使用 E_ALL:

error_reporting(E_ALL);

E_WARNING 的原因

在代码中较早发送标头后尝试发送标头将导致:

PHP Warning:  Cannot modify header information headers already sent in...

另一个常见原因包括试图包含一个不存在的文件:

[php7:warn] [pid 8544] [client 127.0.0.1:57390] PHP Warning:  include(test.php): failed to open stream: No such file or directory in /var/www/test/test.php on line 3
日期:2020-06-02 22:15:29 来源:oir作者:oir