PHP core features – error handling

Time:2021-9-13

Errors and exceptions

Wrong, understandableError in the program itselfFor example, syntax error. The anomaly is more inclined toThe program did not run as expectedperhapsNon compliance with normal process; For the PHP language, the mechanisms used to handle errors and exceptions are completely different, so it is easy to be confused.

For example, we want to handle a divisor of 0 by catching an exception, but PHP triggers an error before the exception is caught.

try {

In other words, PHP triggers the divisor of 0 as an error without automatically throwing an exception, so it can’t catch it. Similarly, in many cases, PHP cannot automatically throw exceptions. Only throughif - elseSentence judgment recombinationthrowMethod and throw an exception manually.

The above situation mainly occurs because the exception mechanism is the product of PHP’s evolution to object-oriented. Before that, PHP reported errors mainly through the error mechanism. Therefore, in many cases, PHP errors are more valuable than exceptions. However, php7 begins to unify the two, so that errors can also be thrown like exceptions (this part will be explained in the exception part).

error level

Errors in PHP can be understood asAn abnormal condition that prevents a script from running properly, it can be divided into five categories according to the error level from high to low

  1. Parse error or syntax error – syntax parsing error. After the error is triggered, the scriptcompletelyUnable to run;
  2. Fatal error – fatal error. After the error is triggered,Later scriptUnable to continue execution;
  3. Warning error – if something is inappropriate, the script canContinue execution
  4. Notice error – there are inappropriate places, but the degree is lower than warning error, and the script canContinue execution
  5. Deprecated error – this is not recommended. It may be discarded in the future. The script can be usedContinue execution

By default, PHP triggers an error and displays the level of the error and the corresponding prompt.

Parse error example – no semicolon at the end of a statement

echo "abc"
// PHP Parse error:  syntax error, unexpected end of file, expecting ',' or ';

Fatal error example – use a function that does not exist

echo "before\n";

Warning error example – import a file that does not exist

$a = "foo";

Notice error example – output variables that do not exist

echo $foo;
echo 12345;
// PHP Notice:  Undefined variable: foo
// 12345

Deprecated error example – pass in a number instead of a string in some string functions

strpos('12345', 3);
// PHP Deprecated:  strpos(): Non-string needles will be interpreted as strings in the future

In addition to the default trigger message, users can also useset_error_handlerFunction to customize error handling. Most error types can be customized exceptE_ERRORE_PARSEE_CORE_ERRORE_CORE_WARNINGE_COMPILE_ERRORE_COMPILE_WARNINGOutside.

set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] ) : mixed

Example

<? php

Users can alsotrigger_errorFunction to manually trigger a user level error(E_USER_ERRORE_USER_WARNINGE_USER_NOTICEE_USER_DEPRECATED)。

function division($a, $b) {

Error related configuration

Some common configurations related to error handling

  • error_reporting-Set error reporting level
  • display_errors-Show errors
  • display_startup_error-Whether to display the display during PHP startup
  • log_errors-Set whether to record the error information of script running to the server error log or error_ Log

Modern PHP proposes four rules

  1. Be sure to let PHP report errors;
  2. Display errors in the development environment;
  3. Errors cannot be displayed in the production environment;
  4. Record errors in both development and production environments;

Recommended configuration of development environment

display_errors = On
display_startup_error = On
error_reporting = -1
log_errors = On

Recommended configuration for production environment

display_ errors = Off

Symfony coding specification

Exception and error message strings must usesprintfTo splice;

throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));

When the error type isE_USER_DEPRECATEDYou need to add@

@trigger_error("foo", E_USER_DEPRECATED);

reference material

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Beautify your code VB (VBS) code formatting implementation code

However, vb.net does have many new functions that VB6 does not have. The automatic typesetting of code is one, which is the function we want to realize today – VB code formatting.Let’s look at the effect: Before formatting: Copy codeThe code is as follows: For i = 0 To WebBrowser1.Document.All.length – 1 If WebBrowser1.Document.All(i).tagName = […]