PHP most detailed from getting started to mastering (v)--php error handling

Source: Internet
Author: User
Tags php error php error reporting syslog system log

PHP from Beginner to proficient

The string in PHP

Error handling is an important part of creating scripts and Web applications. If your code lacks error detection coding, then the program looks unprofessional and opens the door to security risks.

This tutorial introduces some of the most important error detection methods in PHP.

I'll explain the different ways to handle the error:

Error Handling in PHP

1, PHP error level: see the table.
2. Adjust the PHP error reporting level:
There are two ways to adjust the error reporting level in PHP:
① Modify the configuration entry for the php.ini file.
A will cause all PHP files to be affected under the current server environment.
b, if the code to replace the server, will cause the configuration file all expire, need to reconfigure.
So in the actual development process, it is not recommended to use this way!!
② Use the Ini_set () function in your code to dynamically modify the configuration entries for the php.ini file; (Get to get configuration items)

Two important configuration items in the >>>php.ini:
①display_errors: Turns all error level reports on or off;
Optional value: 1/on indicates that all error reporting functions are turned on; 0/off indicates that all reports are closed;
Open by default, and highly recommended!!

code example:

Ini_set ("Display_errors", "0"),----> Close error reporting function;

②error_reporting: Setting those errors can be reported;
There are two functions that can be set: Ini_set () and error_reporting;

code example:

Ini_set ("Error_reporting",e_all); report all levels; error_reporting ("E_all & ~e_notice"); NOTICE level not reported, other normal reports;

3. Use error log to record errors;
① logging using log files:
Use the Error_log () function to output error information to the log file. The default log file is under the log folder in Wamp, Php_error.log file;
>>> Custom log file Print error:

code example:

Ini_set ("Error_log", "e:/text.txt"); // set the log file address; Error_log ("Wrong!!!" "); // print an error message; Error_log ("This is a PHP error", 3, "E:/textlog.text"); // Print the error message directly to the specified file;

② is written into the Windows system log:

code example:

Ini_set ("Error_log", "syslog"); // set logging to the system log; Error_log ("System log Error"); // when using the log error, will automatically enter the system log;

4, Trigger_error: Customize an error, like a system error. Produce an error.
Parameter ①: the wrong information content;
Parameter ②: The level of the error. Changxiang E_user_notice e_user_warring e_user_errors;

5. Custom Error handling function:
① declares an error handler function func () {}
② uses Set_error_handler ("Func"); set, when the file is wrong, no longer use the system's own error prompt function, but instead call our custom function.
>>>set_error_handler in the division function, the function will default to send four parameters: the error level, information, file, the line number;
Therefore, you can accept the error message when declaring the function, and deal with it concretely.

Examples show:

Ini_set("Display_errors", 1); $errMessage=""; functionErrorHandler ($type,$message,$file,$line){        Global $errMessage; Switch($type) {             Case E_notice:$errMessage. = "There is no small bug in the egg, do not have to deal with the <br><br>";  Break;  Case e_warning:$errMessage. = "Error!!! "; $errMessageThe. = "Error type is: {$type}"; $errMessageThe. = "error message is: {$message}"; $errMessage. = "The file where the error occurred is: {$file}"; $errMessage. = "The line number where the error occurred is: {$line}"; $errMessage. = "<br><br>";  Break;  Case E_error:Echo"<script>"; Echo"Location.href=". /01text.php ' "; Echo"</script>";  Break; default:Echo"<script>"; Echo"Location.href=". /01text.php ' "; Echo"</script>";  Break; }            }    Set_error_handler("ErrorHandler"); //ini_set ("error_reporting", E_all); Error_reporting (E_all & ~e_notice);    Ini_set("Error_log", "syslog"); Error_log("This is a PHP error", 3, "E:/textlog.text");Echo $num;//trigger_error ("22222222", e_user_notice);//Custom error, the above function is not accepted, so it will go to default;Echo"11111";Echo"</br>-------------------------------------------------</br>";Echo $errMessage;

Share this issue here, see you next time. Feel content to help you have a friend, please point of recommendation, Welcome to comment below comment area and bloggers interaction, thank you!

Not finished, to be continued ...

For more exciting content, stay tuned for Q's front-end World Tech blog.

PHP most detailed from getting started to mastering (v)--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.