PHP error and exception handling function module Example _php tips

Source: Internet
Author: User
Tags error handling exception handling openlog parse error syslog system log throw exception oracle database

The example in this article describes the PHP error and exception handling function module. Share to everyone for your reference, specific as follows:

First, the type of error and basic debugging methods

Errors in PHP programs are typically attributed to the following three areas:

Syntax error:

Syntax errors are most common and are easy to fix. For example, a semicolon is omitted from the code. This type of error prevents script execution.

Run-time error:

This error generally does not prevent the PHP script from executing, but it will prevent the current thing from being done. Output an error, but the PHP script continues to execute

Logical error:

This error is the most troublesome and does not prevent script execution or output error messages.
An exception is an exception that occurs during the execution of a program, or an event that interrupts the operation of normal instructions and jumps to other program modules to continue execution.

Error reporting level for PHP

E_all//All information value: 6143
e_error//Fatal Run-time error value: 1
E_recoverable_error//Near fatal run-time error, if not captured, treat as E_error value: 4096
E_warning//Runtime Warning (non-fatal error) Value: 2
e_parse//Parse error value at compile time: 4
E_notice//Run-time reminders (often bugs, possibly intentional) values: 8
e_strict//Coding Standardization Warning (recommended how to modify to forward compatible) value: 2048
Fatal error value during initialization of E_core_error//php at startup: 16
E_core_warning//php Warning (non-fatal error) value during startup initialization: 32
E_compile_error//compile-time fatal error value: 64
e_compile_warning//compile-time warning (non-fatal error) value: 128
E_user_error//user-defined fatal error value: 256
E_user_warning//user-defined warning (non-fatal error) Value: 512
E_user_notice//user-customized reminders (often bugs) value: 1024

PHP.ini configuration file

Display_errors: Whether to turn on PHP output error reporting function
Value is: On (default output error report), OFF (block all error messages)
You can invoke the Ini_set () function in the PHP script to dynamically set the php.ini configuration file.
such as: Ini_set ("Display_errors", "on"); Show all error messages
Error_reporting: Sets different error reporting levels.
error_reporting= E_all & ~e_notice
--You can throw any error that is not noticed, the default value
error_reporting= E_error | E_parse | E_core_error
-Consider only fatal run-time errors, new resolution errors, and core errors.
error_reporting= E_all & ~ (E_user_error | e_user_warning | E_user_notice)
--Report all errors except the user-caused error.
In a PHP script, you can set the error reporting level dynamically through the error_reporting () function. such as: error_reporting (E_all);]

Set the error level instance: error.php

 
 

Configuration directives for PHP error reporting behavior

Display_startup_errors= off
Whether to show errors that the PHP engine encountered while initializing.
Log_errors= on
Determines the location of the log statement record.
Error_log (default null)
Specifies incorrectly written files or logging error logs in Syslog.
log_errors_max_len=1024
The maximum length of each log entry, in bytes. 0 represents the largest.

Second, error log

Log error logs in two ways:

Logs the error report log with the specified file
Error log is logged to the operating system log

Logs the error report log with the specified file

1, the first configuration php.ini:

error_reporting= e_all//will send each error to PHP
display_errors=off//does not display error reports
log_errors=on//determines where the log statement is recorded.
log_errors_max_log=1024//the maximum length of each log entry
error_log=g:/myerror.log//Specify error-written files

2, use the function: in the PHP file using Error_log () to log, you can write information to the Myerror.log file

Such as:

Error_log ("Login failed!") ");

3. Use four functions to log logs:

Define_syslog_variables ();//To initialize the configuration Openlog () for the System log ()
//Open a log link
syslog ();//Send a log

Example

<?php
if (! Ora_logon ($username, $password)) {
error_log ("Oracle Database not available!", 0);
Writes the error message to the operating system log
}
if (! $foo =allocate_new_foo ()) {
Error_log ("There is a big problem!", 1, "webmaster@www.mydomain.com");//Send to admin mailbox
Error_log ("Screwed Up!", 2, "localhost:5000");
error_log ("Screwed Up!", 3, "/usr/local/errors.log")
sent to the server corresponding to the 5000 port on this machine. Send to the specified file
?>

<?php
define_syslog_variables ();
Openlog ("PHP5", Log_pid, Log_user);
Syslog (log_warning, "warning report sent to syslog in demo, warning Time:". Date ("Y/m/dh:i:s"));
Closelog ();
? >

To view logs: such as Windows system, choose the admin option by right-clicking My Computer->-> Select Event Viewer in the System Tools menu-> See the Log in the application options

Iii. Exception Handling

An exception (Exception) handles the normal process used to alter a script when a specified error occurs. is a new and important characteristic in PHP5. Exception handling is an extensible and maintainable mechanism for error handling, and provides a new object-oriented error-handling method.

Exception handling Format:

try{
//Use try to contain code that might cause an exception.
Once an exception try is caught, the catch processing is given.
//Throw exception statement: Throw exception object.
}catch (Exception object parameter) {
//exception handling here.
} [Catch (. ,,) {
...
}]

More interested in PHP related content readers can view the site topics: "PHP error and Exception handling method summary", "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP object-oriented Programming Introductory Course", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.