PHP Error Handling

Source: Internet
Author: User
Tags php error

PHP Error Handling


Classification of errors:


Syntax error: program cannot run, direct prompt syntax error

Run-time error: An error that occurs only if the program runs to a row, or is run in a particular situation.

Logic error: The program ran from start to finish without (and prompting) the error, but the result of the program run calculation is incorrect.


The main thing programmers face and the error to handle is a run-time error.


Wrong rating:


In PHP, a variety of errors, categorized, according to different serious programs and the source (mechanism), the various errors are divided into about more than 10 levels.

Each level of error corresponds to an internal name-system constant!


System error:


E_error System Critical Error (one occurs, the program stops executing immediately.) This error is generally expected to be resolved immediately)

e_warning System Warning (one occurs, prompts for errors, and continues execution.) Usually this error is expected to be dealt with "next business day".

E_parse syntax error (one occurs, an error is indicated, and the code does not run at all-check the syntax before running.) )

E_notice system Prompt (one occurs, prompts for errors, and continues execution.) Send an e-mail notice, oneself schedule time to solve. )


What is the nature of them?


echo "<br/>e_error =". E_error;

echo "<br/>e_warning =". e_warning;

echo "<br/>e_parse =". E_parse;

echo "<br/>e_notice =". E_notice;


The results of the output were:


E_error = 1
E_warning = 2
E_parse = 4
E_notice = 8


They are only constants within a system (integer constants); in fact, these constants (codename) are identifiers for our "control" of such errors.


User-defined error:

There are only three of them:

E_user_error

E_user_warning

E_user_notice

We can create (generate) errors ourselves in the program--errors created for the unreasonable circumstances of some data

For example: Let the user fill in the age, fill 18 is OK, but fill 188, it is unreasonable-from the digital angle itself is no problem


Other errors:

E_all represents all errors--indicating that it can "include" all errors.

E_strict represents a "strict" syntax check error--a syntax that can be executed, but is not welcome in the current PHP version.


Incorrect triggering:

Normal Trigger: a run-time error occurred while the program was running--it could also be a syntax check error

Three common run-time errors:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/76/10/wKioL1ZJnXnSu9CzAACTQZViLYY360.jpg "title=" 01.jpg "alt=" wkiol1zjnxnsu9czaactqzvilyy360.jpg "/> Manual trigger:

Is that the programmer "generates" an error through the program code.

Grammar:

Trigger_errer ("Error Prompt", user error code);

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/76/12/wKiom1ZJnpaQ-UVRAACDh-lKBug931.jpg "title=" 01.jpg "alt=" Wkiom1zjnpaq-uvraacdh-lkbug931.jpg "/>

Applications, it is often necessary to detect (judge) the validity of the data to determine whether to trigger the corresponding error, such as:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/76/10/wKioL1ZJn-WTw9RNAAAzRD8Jfds859.jpg "title=" 01.jpg "alt=" Wkiol1zjn-wtw9rnaaazrd8jfds859.jpg "/>


Error Display control:

Web page display (hint) error message, can be controlled, there are two ways:

1: Set in php.ini file, valid for all PHP programs

2: In the current script file, only valid for the current script file

The error display has 2 aspects that can be controlled:

1: Set whether to display: Display_error

PHP.ini control: Display_error = on; Indicates display, off means no display

In the script: Ini_set ("Display_error", 1); 1 indicates display, 0 means not displayed, or false with true or

The settings in the script have a high priority, but only valid for the current file.

2: Set what level of error is displayed--dependent on the condition that the first item is set to display

In the php.ini file:

error_reporting = E_notice; Show only E_notice errors

error_reporting = E_notice | E_warning//Show E_notice and E_warning

error_reporting = E_notice | e_warning | E_error//Show E_notice and e_warning and E_error


Ini_set () meaning can be set in almost all php.ini, in the form of the following:

Ini_set ("Set Item Name", value); This setting is only valid for the current script, and without restarting Apache, it is convenient

Another corresponding function is: Int_get ("Set item Name"); The value used to get an item.


Logging error logs:

In the development phase, we usually show all errors--intent to resolve the error

In the product phase, we usually hide all errors--and log the error message to the file--error log file

We can view the error log ourselves to resolve errors that were not found during the development phase

There are also 2 ways to log error logs:

1: Set in the php.ini file

Log_errors = on; Used to set whether the error log is logged, on or off

Error_log = "Error log file name"; such as: Error_log = Php_errors.log

At this point, the file does not have a given path, and the file is created under each folder and recorded

Error_log also has a special value that can be used: Error_log = syslog; Indicates that the error log file is not logged, but instead writes the error message to the system error log

2: In the current script file


PHP Error Handling

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.