PHP Error Handling

Source: Internet
Author: User
Tags date exit error handling execution ini log php error php error log

What happens if the program does not have an error-handling mechanism? There may be problems at times.

Example: A more robust program:

<?php
if (!file_exists ("Aa.txt")) {
echo "file does not exist";
Exit ();
}else{
$FP =fopen ("Aa.txt", "R");
echo "File open successfully";
Fclose ($FP);
}
?>

How PHP handles the error

1, simple "die ()" statement.

Die () function is equivalent to exit ()

<?php
/*if (!file_exists ("Aa.txt")) {
Die ("file does not exist");
}else{
$FP =fopen ("Aa.txt", "R");
echo "File open successfully";
Fclose ($FP);
}*/
The following code is the same as the code in the multiline comment above, but it is much simpler
File_exists ("Aa.txt") or Die ("file does not exist"); If the function is true, the program continues execution, otherwise the execution die ()
$FP =fopen ("Aa.txt", "R");
echo "File open successfully";
Fclose ($FP);
?>

2. Custom error (Error handler) and error trigger

The function must be capable of handling at least two parameters (Error level and error message), but can accept up to five parameters (optional: File,line-number and the error context):

Basic syntax:

Error_function (Error_level,error_message,error_file,error_line,error_context);

Error level of PHP

Cases:

<?php
Custom error-handling functions
function My_error ($errno, $errmes) {
echo "<font size= ' 5 ' color= ' Red ' > $errno </font><br/>";
echo "error message is: $errmes";
Exit ();
}
Reformulation Set_error_handler Processor
Set_error_handler ("My_error", e_warning); Specify warning-level error calls custom error functions
$FP =fopen ("Bb.txt", "R");
?>

PHP error triggers: for handling logic errors

For example: There is a section of code that receives an age if the input age >=120 is considered an error.

Traditional methods

if ($age >=120) {
echo "Too old";
Exit ();
}

<?php
Custom Error processor
function My_error3 ($errno, $errmes) {
echo "Error number is:". $errno;
}
Specify E_warning-level error-handling functions
Set_error_handler ("My_error3", e_user_warning);
$age = 300;
if ($age >120) {
Invoke the trigger, specifying the error level
Trigger_error ("The age of input is too large", e_user_warning);
Exit ();
}
echo "OK";
?>

Example: Customizing an error-handling function that automatically calls the function when a file does not exist, and logs the error message, asking for the error number, error message, and time of occurrence.

3. PHP error Log

By default, depending on the error_log configuration in php.ini, you can send an error record to the specified file or remote destination by using the Error_log () function.

Grammar:

BOOL Error_log (string $message [, int $message _type=0[,string $destination [, String $extra _headers]]])

Cases:

<?php
Custom Error processor
function My_error3 ($errno, $errmes) {
$err _info= The error number is: ". $errno." -". $errmes;
Save the error message
Indicates that a carriage return line is exported to the file
Error_log ($err _info.) \ r \ n ", 3," myerr.txt ");
}
Specify E_warning-level error-handling functions
Set_error_handler ("My_error3", e_user_warning);
$age = 300;
if ($age >120) {
Invoke the trigger, specifying the error level
Trigger_error ("The age of input is too large", e_user_warning);
Exit ();
}
echo "OK";
?>

Now it's time to save:

PHP Set time zone

<?php
Echo Time (). <br/> ";
Output current date and time
The default is UTC, which differs from China by 8 hours, setting the time zone method:
1, Page Setup 2, set in php.ini
Date_default_timezone_set ("PRC");//Set to China time zone
echo "<br/>";
echo Date ("Y-m-d g-i-s")
?>

Reformulation The example, so that the log can also record the time

<?php
Custom Error processor
function My_error3 ($errno, $errmes) {
$err _info= The error number is: ". $errno." -". $errmes;
Save the error message
Indicates that a carriage return line is exported to the file
Date_default_timezone_set ("PRC");//Set to China time zone
Error_log ("Time is". Date ("Y-m-d g-i-s"). $err _info. " \ r \ n ", 3," myerr.txt ");
}
Specify E_warning-level error-handling functions
Set_error_handler ("My_error3", e_user_warning);
$age = 300;
if ($age >120) {
Invoke the trigger, specifying the error level
Trigger_error ("The age of input is too large", e_user_warning);
Exit ();
}
echo "OK";
?>

Url:http://www.bianceng.cn/webkf/php/201701/50504.htm

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.