Home >Backend Development >PHP Problem >Let's talk about the related configuration of PHP error prompts

Let's talk about the related configuration of PHP error prompts

PHPz
PHPzOriginal
2023-04-04 16:13:13545browse

PHP is a popular global open source scripting language that is widely used to build dynamic websites and web applications on the server side. PHP provides built-in error handling functionality. If you do not configure PHP error reporting appropriately, you may face many debugging difficulties. In this article, we will explore the relevant configuration of PHP error prompts.

  1. PHP Error Types

PHP defines different types of errors:

  • Fatal Error (E_ERROR): This is the failure of the PHP parser The most severe type of error to continue execution. It causes the program to abort execution.
  • Strict warning (E_STRICT): This type of error can occur when you use a function or method that does not exist or is obsolete.
  • Syntax Errors (E_PARSE): These are syntax errors found while parsing the code.
  • Warning (E_WARNING): Information about problems discovered when PHP interacts with your code.
  • Notification (E_NOTICE): This error indicates that some variables have not been initialized and they need to be assigned initial values ​​before executing them.
  1. Error prompt level

In PHP, you can control the level of detail of error reporting. PHP provides four different reporting levels for error prompts:

  • Error reporting (E_ERROR | E_WARNING | E_PARSE): This is the most serious error reporting level, which prompts all PHP errors.
  • Strict error reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_STRICT): This level prompts all PHP errors, including notifications and strict warnings.
  • Other reporting levels (E_ALL & ~E_NOTICE): This level prompts all PHP errors except notifications.
  • Level of production reporting (E_ERROR): This level will only prompt the most serious errors.
  1. Error log

The error log is another important configuration of PHP. When PHP code executes, it may generate errors, and these errors are logged in the error log, which is useful for debugging.

In the apache server, you can configure the error log in php.ini. The following code logs errors to "/var/log/php_errors.log":

error_log = /var/log/php_errors.log

If you do not have access to php on the server. ini file, you can use the following code in your PHP code to configure the error log.

ini_set('error_log', 'my_error_log.log');

  1. Display errors

In a production environment, you may not need to An error message is displayed. In this case, you can turn off the error message on the page. To turn off error messages on the page, you can find the following code in php.ini:

display_errors = On

Change it to

display_errors = Off

Alternatively, use the following code to turn it off in code:

ini_set('display_errors', 0);

  1. Handling errors

In In PHP, you can use the set_error_handler() function to customize the error handler. For example, the following code uses a custom error handler to log errors and give the user a friendly error message:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{

error_log($errno.': '.$errstr.' in '.$errfile.' on line '.$errline);
echo ‘Sorry, something went wrong. Please try again later.’;

}

set_error_handler("myErrorHandler");

When an error occurs in your code, the myErrorHandler() function is called, which records the error and outputs a friendly error to the user information.

Summary

In PHP, error handling is important because it helps you avoid many debugging problems. You can control PHP error messages by changing error message levels, configuring error logging, and customizing error handlers. Make PHP work better, I hope this article will be helpful to you.

The above is the detailed content of Let's talk about the related configuration of PHP error prompts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn