PHP error handling and the exception handling mechanism of PHP

Source: Internet
Author: User
Tags contains error handling exception handling function definition log php error syslog system log

PHP Error Handling

When we develop programs, sometimes there are problems with the program, we can use the following methods to find the error.

Development phase: Output all error reports during development, which will help us to debug the program

Run phase: We do not let the program output any kind of error report (not to allow users to see (including technical, not skilled people))

Write error reports to the log

First, specify error report error_reporting = E_ll

Second, turn off error output Display_errors = Off

Third, turn on error logging function log_errors = On

1. Default if no error log location is specified, the default write Web server log

2. Specify a filename (writable) for the Error_log option

3. Write to the operating system log Error_log=syslog

The following code example

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><?php  
//  error_reporting (e_all);  
Ini_set ("Display_errors", "off");  ini_set ("Error_log", "syslog");  ini_set ("Max_fileupload", 200000000);  Echo ini_get ("upload_max_filesize");  Error_log ("This is an error message!!!!");  
    GetType ($var);   Note  
    GetType ();  Warning  
    gettye ();  The error  terminates the program running  
    echo "###########################<br>";   
? ></span>

Of course, PHP also provides function error_get_last () to get the error message function definition and usage

The Error_get_last () function Gets the last occurrence of the error.

The function returns the last occurrence of the error as an array.

The returned array contains 4 keys and values:

[Type]-error type

[Message]-error messages

[File]-Files where the error occurred

[Line]-where the error occurred

Small example:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><?php Echo $test; Print_r (Error_get_last ());?> output:  
Array ([type] => 8 [message] => Undefined variable:test [file] => D:\WW w\test.php [line] => 2) </span>

php5.4 also provides PHP predefined variables later $php_errormsg

$php _errormsg-a previous error message

$php _errormsg variable contains the latest error message generated by PHP. This variable is only available within the scope of the error occurrence and requires that the Track_errors configuration entry be turned on (by default).

Example:

<?php  
@strpos ();  
echo $php _errormsg;  
? >

Will output:

Wrong parameter count for Strpos ()

So it's convenient for us ... Is it helpful to debug your program and troubleshoot bugs?

These error reporting levels are different types of errors that the error handlers are designed to handle:

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.