首页  >  文章  >  后端开发  >  PHP 中的 error_reporting()

PHP 中的 error_reporting()

王林
王林原创
2024-08-29 13:06:15202浏览

在 PHP 的各种级别的错误中,error_reporting 是 PHP 中的一个函数,它指示报告的错误是什么,并确定运行时的 error_reporting 指令。使用此函数,我们可以为脚本所需的持续时间(通常是运行时)设置规定的级别。它根据给定的输入返回旧的错误报告级别,或者在未给出参数时返回当前的报告级别。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

带参数的语法

以下是带参数的语法:

语法:

error_reporting(level)

参数:

只有一个参数级别是可选的并且其输入函数采用。它指定当前脚本的错误报告级别。接受的值为常量名称和值编号。

注意: 为了确保 PHP 未来版本的兼容性,建议使用命名常量。

有一些预定义常量,其说明如下:

1。 E_Error:这些表示无法恢复的致命运行时错误,并且脚本执行将停止。

2。 E_Warning: 这些是非致命错误,脚本将继续执行。

3。 E_Parse: 这显示仅由解析器生成的编译时解析错误。

4。 E_Notice:这会发出运行时通知,表明脚本发现了一些显示错误的内容,但在运行正常脚本时也可能会发生这种情况。

5。 E_Core_Error: 在 PHP 初始启动期间,可能会出现一些由 PHP 核心生成的致命错误。

6。 E_Core_Warning:这显示了 PHP 初始启动期间出现的非致命错误,也是由 PHP 核心生成的。

7。 E_Compile_Error:这些显示编译期间发生的致命错误。这些是由 Zend 脚本引擎生成的。

8。 E_Compile_Warning:与上面类似,这些显示编译时警告或可以称为非致命错误,也由 Zend 脚本引擎生成。

9。 E_User_Error:显示用户生成的错误。这与 E_ERROR 类似,只不过它是使用 PHP 代码中的 PHP 函数生成的。

10。 E_All:这就像上述所有内容的组合,支持除 E_STRICT 之外的所有错误和警告。

返回值:

如果没有给出参数,error_reporting 函数会给出旧的报告级别或当前的错误报告级别。

PHP 中 error_reporting 的工作原理

此功能允许开发人员实际控制不同类型的错误以及应用程序中将抛出多少此类错误。此函数设置一个 error_reporting 指令,该指令将出现在 PHP ini 配置文件中。

error_reporting(0);
  • When 0 is passed to the error reporting function it removes all warnings, errors, parse related messages and notices, if any. Instead of having to include this line in each of the PHP code files, it is practical to have it added and to turn off these report messages in the ini file present or in the .htaccess.
error_reporting(E_NOTICE);
  • In PHP the variables can be used even when not declared. But this practice is not feasible as the undeclared variables may cause application related issues if it is used in conditional statements and loops. This may also take place because of the spelling mismatch between the declared variables and of that being used for conditions and loops. When this E_NOTICE will be passed into the error_reporting function, only then these undeclared variables will be shown in the web application.
error_reporting(E_ALL & ~E_NOTICE);
  • This error reporting function helps to filter out the errors which can be displayed. The “~” character here means the “not/no” and hence ~E_NOTICE here means to not show any notices. Here the “&” character represents “true for all” whereas “|” means as long as one of the parameters is true. They are exactly similar to the functions AND and OR in PHP.
error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);
  • All of the above lines serve the same purpose i.e. show all the errors. E_ALL is the most widely used function among all others by developers to display error messages as it is more comprehensible and intelligible.

Error Logging in PHP using error_log() Function

It happens so that during the production phase, error messages are to be hidden from the end-users but this information is needed to be registered for tracing purpose. And the best way to record these errors on the production web application is to write and store in log files.

An easy way to log these is by using the error_log function which takes our parameters as input. The only mandatory parameter here is the first one which contains details about the errors and what all to be logged. Other parameters like the type, destination, and header are non-mandatory here for this function.

error_log("Error found!", 0);
  • The type parameter will be set to 0 by default if not given, and the log information will be appended at the end of the log file generated in the webserver.
error_log("Error information being emailed!", 1, "[email protected]");
  • The type parameter here is 1 will email this log specified in the 3rd parameter which is the email id. For this to work, the PHP ini file must be having a correct SMTP configuration to send out emails. Some of the parameters required for these include host, encryption type, port, password and username.
error_log("Write errors to this file", 3, "https://cdn.educba.com/tmp/errorfile.log")<em>;</em>
  • The same error logs can also be written down to the required file whose path will be given in the third parameter. Make sure the given path has all required permissions.

Example of error_reporting() in PHP

Given below is the example:

Code:

<?php
$a = 1;
trigger_error("user warning!", E_USER_WARNING);
$a = 2;
echo "Value of $a is ${$a}";
error_reporting(0);
error_reporting(E_ALL);
?>

Output:

PHP 中的 error_reporting()

Advantages of using error_reporting function in PHP

  • error_reporting is good for debugging purposes and for developing web application.
  • Each and every error can be logged and fixed as soon as it happens using this function.
  • To not show it to the end-user, make sure you redirect the errors to a log file while releasing it.

Conclusion

Hence we can say that error_reporting() function in PHP are therefore helpful in cases when there are a lot of problems with the PHP web application and we need to display all of these errors and warnings either for development or debugging purposes. It is a function we can enable different kinds of warnings or error messages and most of them are as discussed above.

以上是PHP 中的 error_reporting()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP Custom Exception下一篇:PHP Log Errors