Handling of PHP errors

Source: Internet
Author: User
Tags php language php script

Classification of errors

Usually divided into 3 kinds:

Syntax error:

Before the program runs, check the syntax first. If there are errors in the grammar, the error will be immediately, and the program will not be executed.

Run-time error:

That is, after the program syntax check is passed, start running the program and encounter errors in this process. Common in 3:

Prompt error:

Warning Error:

Fatal error:

Logic error:

Refers to the program itself can be executed normally, no error--but the "calculation result" is wrong.

Wrong rating

In the PHP language, various errors are categorized in different levels, and there are about 10 levels of errors, which is the technical level of error grading.

Each level of error, there is a "code", the code is actually a system inside the "constant just". Like what:

Common system errors:

E_error: Fatal error

E_warning: Warning Error

E_notice: Suggestive Error

User-Customizable Error:

E_user_error: Custom Fatal error

E_user_warning: Custom Warning Error

E_user_notice: Custom Prompt error

Other:

E_strict: Rigorous grammar check error

E_all represents "all errors".

Detailed reference manual:

Here's a look at the actual values of these error codes:

The result of the operation is:

Wrong trigger.

The wrong trigger is to let the error "happen".

There are two ways of triggering an error:

System Trigger

When the program runs to a line of code, there is a certain error, and the system will error-that is, it triggers the system errors.

There are 3 typical errors that are triggered by the system:

E_notice: Prompt error: Will output error prompt, and continue to execute subsequent code;

For example, use a non-existent variable or constant:

E_warning: Warning Error: Error prompt is output, and subsequent code execution continues (possibly depending on the situation, such as require)

For example, include loading a nonexistent file:

E_error: Fatal error: Causes the program to not execute subsequent statements;

Call a nonexistent function, for example!

Custom Trigger:

When we are dealing with some data, the data itself is not wrong, but according to the needs of the specific application (business), the data will be required to meet a certain condition, and the data is not satisfied, we can "active" in the program to trigger (create) An error to indicate the "illegality" of the data.

Grammatical form:

Trigger_error ("error message Content", one of the user error codes in 3);

The user's fatal error (E_USER_ERROR) is triggered and subsequent execution of the program is terminated.

Display problems with Error reporting

The so-called error report is the error message displayed on the webpage!

For error reporting, there are 2 issues that need to be addressed:

Whether to display error reports (display_errors):

There are 2 ways to set whether to display:

Procedure 1:

In the php.ini file, set the value of the Display_erros to On (display), or off (not shown)

Can be modified to:

Of course, as a development phase, we should all display error messages.

Note: The prerequisites are that we Apache has loaded the php.ini file-This requires the following line to be added to the Apache configuration file Httpd.config:

Phpinidir "php.ini file location (path)"

Like what:

Method 2:

Use the function Ini_set () to set it directly in the php script file:

Of course, if set to 1, it is displayed!

Attention:

1, regardless of the form, the word is the same: display:errors

2, the use of php.ini configuration, the impact of the global (that is, all PHP pages);

3, using the Ini_set () setting in a script code only affects the script code itself-this is the usual way.

4, the settings in the script take precedence over the settings in php.ini.

What level of error Reporting (error_reporting) is displayed:

Obviously, the premise is that "Display_errors" is set to On (or 1), indicating that it can be displayed.

There are also 2 ways to show which levels of error reporting are:

Procedure 1 : in PHP.ini in the document;

This value currently represents "all errors" and is displayed.

Modified to:

More examples are:

error_reporting = E_notice | e_waring | E_error//show the 3 kinds;

error_reporting = E_error | E_user_error//display of the 2 serious errors

To represent the true "all errors", it should be written as: E_error | E_strict, pictured below:

Procedure 2 : In the current script code:

As in the php.ini settings, here are some examples:

Ini_set ("error_reporting", E_notice); This level of error is displayed

Ini_set ("error_reporting", E_notice | e_warning),//show 2 levels

Ini_set ("error_reporting", E_notice | e_warning | E_ERROR),//show 3 levels

Ini_set ("error_reporting", E_all | E_STRICT),//This only represents the display of all errors!

Logging Issues for error logs

The error log is actually an error report, except that it is "written to the file" and is now called the error log!

There are also 2 questions, and there are 2 approaches to each problem:

Whether to record log_errors:

In php.ini:

Log_errors = ON or Off

In the script:

Ini_set ("Log_erros", 1); or 0

Add a sentence:

1:ini_set ("PHP config item", value);//used to set the value of an item in php.ini in the script.

2,: $v 1 = ini_get ("PHP config item");//To get the value of an item in php.ini

Where to log error_log:

There are generally only 2 words:

Notation 1: Use a file name directly, and the system automatically creates the file name under each folder and uses it to record the error message that occurs with all the Web page files under that folder.

It then executes a webpage with errors and can observe:

The content is about:

Notation 2: With a special name "Syslog", all error messages are logged to the system's "log file" at this time.

The system log file is here: Control Panel Administration Tool Event Viewer window log application:

Custom Error handlers

What is the error handler?

is a "machine" that is used to handle the error once an error occurs-it is actually a function.

Custom error handling refers to:

Let the system not handle the error, and we (the developer) to deal with the error: Display and record.

Practice, in fact very simple, on 2 steps:

The first step:

Set the name of the function to use for handling errors!

Set_error_handler ("F1");

Step Two:

To define the function!

Function F1 () {

Here you can write code: Naturally normal is to display error reports, and log error logs.

}

Handling of PHP errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.