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

What are the ways to deal with PHP errors and anomalies?

2025-02-22 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 "PHP errors and methods for dealing with different lengths." In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

With logging, you can send information directly to other log servers, or to designated email addresses (or via mail gateways), or to operating system logs, etc., so that you can selectively log and monitor the most important parts of your applications and websites.

Error reporting allows you to customize the level and type of error feedback, either as a simple prompt or using custom functions to process and return information.

Why use error handling?

1. Be user-friendly when the website goes wrong

2. Better error avoidance, debugging, and bug fixing

3. Avoid some security risks

4. Better guarantee the robustness of the program

5.……

The simplest error handling-die()

When we expect an error to occur, we stop running. For example, when connecting to a database:

The copy code is as follows:

mysql_connect ('localhost ', ' root','123456') or die ('connect database error:'. mysql_error());

However, simply terminating scripts is not always the right way to do it.

Second, custom errors and error triggers

We create a special error-handling function that can be called when an error occurs in PHP, set with the set_error_handler function.

1. Parameters defining error handling functions:

Parameter Description error_level Required. Specify the error reporting level for user-defined errors. Must be a value.

See the table below: Error Reporting Levels.

error_message required. Specify error messages for user-defined errors. error_file Optional. Specifies the file name in which the error occurred. error_line Optional. Specifies the line number at which the error occurred. error_context Optional. Specify an array containing each variable and its value that was in use when the error occurred.

2. Error Basic predefined constants:

Value Constant Description Remarks 1E_ERROR (integer) Fatal runtime error. Such errors are typically nonrecoverable, such as memory allocation problems. The consequence is that the script terminates and no longer runs.

2E_WARNING (integer) Runtime warnings (non-fatal errors). Only prompt information is given, but the script does not terminate.

4E_PARSE (integer) Compile time syntax parsing error. Parsing errors are generated only by the parser.

8E_NOTICE (integer) Runtime notification. Indicates that the script encountered a situation that might appear as an error, but similar notifications may be present in scripts that work.

16E_CORE_ERROR (integer) Fatal error during PHP initialization startup. This error is similar to E_ERROR, but is generated by the PHP engine core. since PHP 432E_CORE_WARNING(integer)PHP initializes warnings (non-fatal errors) that occurred during startup. Similar to E_WARNING, but generated by the PHP engine core. since PHP 464E_COMPILE_ERROR(integer) Fatal compile-time error. Similar to E_ERROR, but generated by the Zend scripting engine. since PHP 4128E_COMPILE_WARNING(integer) Compile-time warnings (non-fatal errors). Similar to E_WARNING, but generated by the Zend scripting engine. since PHP 4256E_USER_ERROR (integer) User-generated error messages. Similar to E_ERROR, but generated by the user himself using the PHP trigger_error() function in his code. since PHP 4512E_USER_WARNING(integer) User-generated warning messages. Similar to E_WARNING, but generated by the user himself using the PHP function trigger_error() in his code. since PHP 41024E_USER_NOTICE(integer) User-generated notification information. Similar to E_NOTICE, but generated by the user himself using the PHP function trigger_error() in his code. since PHP 42048E_STRICT (integer) Enables PHP suggestions for code modifications to ensure optimal interoperability and forward compatibility. since PHP 54096E_RECOVERABLE_ERROR(integer) fatal error that can be caught. It indicates that a potentially dangerous error has occurred, but it has not caused the PHP engine to become unstable. If the error is not caught by the user-defined handle (see set_error_handler()), it becomes an E_ERROR and the script terminates. since PHP 5.2.08192E_DEPRECATED (integer) Runtime notification. When enabled, warns against code that may not work properly in future releases. since PHP 5.3.016384E_USER_DEPRECATED(integer) Warning message about user shortage. Similar to E_DEPRECATED, but generated by the user himself using the PHP function trigger_error() in his code. All error and warning messages outside E_STRICT since PHP 5.3.030719E_ALL (integer). 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously

(Level E_ERROR, E_USER_ERROR cannot be caught by custom error handling functions) Fatal error messages cannot be caught in custom error handling functions, because scripts stop execution immediately when fatal runtime errors occur.

3. trigger an error

The location in the script where the user enters data, useful for triggering errors when the user's input is invalid. In PHP, this task is done by trigger_error().

You can trigger an error anywhere in the script, and by adding a second parameter, you can specify the error level to trigger.

4. Possible error types:

E_USER_ERROR -fatal user-generated run-time error. Error cannot be recovered. Script execution interrupted.

E_USER_WARNING -Non-fatal user-generated run-time warnings. Script execution is not interrupted.

3).E_USER_NOTICE -Default. User-generated run-time notifications. The script found a possible error, which could have occurred while the script was running correctly.

For example:

The copy code is as follows:

trigger_error("error", E_USER_WARNING);

//Output Warning: Error message in xxxx

III. Error reporting

By default, PHP sends error logs to the server's error logging system or file, according to the error_log configuration in php.ini.

By using the error_log() function, you can send error records to specified files or remote destinations. For example, sending error messages to the mailbox is a good way.

For more error handling documentation see www.php.net/manual/zh/book.errorfunc.php

IV. Exception Handling

When an exception is thrown, the code that follows it does not continue, PHP tries to find a matching "catch" code block.

If the exception is not caught and is not handled appropriately with set_exception_handler(), a fatal error occurs and an "Uncaught Exception" error message is output.

1. The processing procedure shall include:

1.)try -The function that uses the exception should be inside the "try" code block. If no exception is triggered, the code continues as usual. But if an exception is triggered, an exception is thrown.

2.)throw -This specifies how exceptions are triggered. Each "throw" must correspond to at least one "catch"

3.)catch -The "catch" code block catches the exception and creates an object containing the exception information

2. Rethrow exception

Sometimes, when an exception is thrown, you might want to handle it differently than standard. Exceptions can be thrown again in a "catch" block of code.

Scripts should hide system errors from users. System errors may be important to programmers, but users are not interested in them. To make it easier for users to use, you can again throw exceptions with user-friendly messages.

3. Rules for exceptions

1). Code that requires exception handling should be placed inside the try code block to catch potential exceptions.

2). Each try or throw block must have at least one catch block.

3). Use multiple catch blocks to catch different kinds of exceptions.

4). You can throw a (re-thrown) exception again in the catch block inside the try block.

"PHP error and what are the methods of dealing with different length" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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

Development

Wechat

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

12
Report