In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the errors and anomalies in PHP7". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Systematic understanding of errors and exceptions in PHP7
One of the reasons why PHP language is simple is the error handling mechanism of PHP. With the increasing modernization of PHP language, there are exceptions. This blog simply describes errors and exceptions for systematic understanding. In addition, for any language, the existence of exceptions has something in common, so it is necessary to learn a language to understand exception mechanisms.
What is a mistake?
When PHP language encounters abnormal situations (such as failure of database connection or error in passing function parameters), some errors will be reported. Errors can be divided into many types. Except for E_ERROR and E_CORE_ERROR errors, other errors will not terminate the program.
The reason why PHP makes people feel simple is that programs do not report errors frequently, giving people the illusion of smooth and convenient writing.
It is precisely for this reason that the rigor and accuracy of the PHP program are much worse. For example, when a mysql_fetch_array query encounters a network error and returns FALSE (the program does not stop running), if the calling program thinks that the query does not have matching data, then the program is essentially wrong.
Through the php.ini instruction error_reporting or dynamically calling the error_reporting () function, we can choose what type of error to report, and through the display_errors instruction we can control whether the error is output online. The error_log instruction can control the output of errors to the log.
How to use errors correctly
Whether it is a system function or a custom function, if an internal error is encountered, how do you tell the caller? It is generally indicated by the function returning TRUE or FALSE. This approach has several disadvantages:
The ● caller only knows that an error has occurred, but returns too few error messages and lacks a description of the error type
● program processing logic and error handling are mixed together, the resulting code will be very unclear.
One tip: the error_get_last () function returns the specific cause of the recent error.
Best practices:
● set_error_handler () function to host all errors
The ● trigger_error () function can trigger custom errors and can be used to replace return statements in the function.
● outputs all errors to the log and defines the error type
● displays errors to users, such as returning errors to users in a more friendly way
The display_errors instruction is turned off in the ● production environment, while it is turned on in the development environment.
The way the old PHP framework Codeigniter handles errors can be used for reference.
`function _ error_handler ($severity, $message, $filepath, $line) {$is_error = (E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) = $severity); / / output 500 error HTTP status code if ($is_error) {set_status_header (500) } / / break if directly (($severity & error_reporting ())! = $severity) {return;} / / log all errors in the log $_ error = & load_class ('Exceptions',' core'); $_ error- > log_exception ($severity, $message, $filepath, $line) / / friendly output of all errors if (str_ireplace (array ('off',' none', 'no',' false', 'null'),', ini_get ('display_errors') {$_ error- > show_php_error ($severity, $message, $filepath, $line);} / / exit if ($is_error) {exit (1) directly if fatal error }} set_error_handler ('_ error_handler'); `
What is an anomaly?
An exception is also an error, which has the following characteristics:
● exceptions can be customized, SPL provides many types of exceptions, you can also extend it
The most common action of ● exception is catching, so that developers can deal with it later according to specific error. For example, you can return friendly prompts to the user according to the exception context. Or continue to throw an exception and let the upstream program handle it. If no exception is caught, the program terminates directly.
● exception another action is to throw, if you write business logic through the function, encounter unexpected circumstances, you can directly throw an exception.
● exceptions can be caught by the code layer by layer. If the outermost program has not been caught, the code will stop running directly.
If an exception in ● PHP cannot be caught, it is written to the system error log as a fatal error
Explain it through intuitive code:
`function inverse ($x) {if ($x < 10) {throw new Exception ('xx 10 and x)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.