Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Handling of PHP errors and exceptions

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

PHP error and exception handling, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Errors and exceptions

Errors, you can understand the errors of the program itself, such as syntax errors. On the other hand, exceptions are more inclined to run programs that do not meet expectations or do not conform to normal processes; for PHP language, the mechanisms used to handle errors and exceptions are completely different, so it is easy to cause confusion.

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

Try {$a = 5 / 0;} catch (Exception $e) {$e-> getMessage (); $a =-1; / / handle the case where $an is 0 with an exception, but in practice, the exception} echo $a _ bang / PHP Warning: Division by zero cannot be caught.

In other words, PHP triggers a situation with a divisor of 0 as an error and does not automatically throw an exception, so it cannot be caught. Similarly, in many cases, PHP cannot automatically throw an exception. You can only use the if-else statement to determine and combine the throw method and manually throw an exception.

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

Error level

Errors in PHP can be understood as situations that make the script not run properly and can be divided into five categories according to the error level from high to low.

Parse error or Syntax Error-syntax parsing error, which causes the script to fail to run at all

Fatal Error-fatal error, after the error is triggered, subsequent scripts cannot continue to execute

Warning Error-where something is inappropriate, the script can continue to execute

Notice Error-there is something inappropriate, but to a lower degree than Warning Error, and the script can continue to execute

Deprecated Error-this is not recommended and may be abandoned in the future. Scripts can continue to be executed.

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

Parse Error example-semicolons are not written at the end of the statement

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

Fatal Error sample-use a function that does not exist

Echo "before\ n"; foo (); echo "after"; / / the line cannot continue / / before// PHP Fatal error: Uncaught Error: Call to undefined function foo ()

Warning Error example-introducing a file that does not exist

A = "foo"; include ('bar.php'); echo $a; / / Program continues / / PHP Warning: include (bar.php): failed to open stream: No such file or directory. / / foo

Notice Error sample-output variables that do not exist

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

Deprecated Error sample-pass numbers instead of strings 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 use the set_error_handler function to customize error handling. Most error types can be customized, except E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, and E_COMPILE_WARNING.

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

Example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report