Home  >  Article  >  Backend Development  >  Detailed explanation of how to obtain PHP error logs, detailed explanation of how to obtain PHP logs_PHP tutorial

Detailed explanation of how to obtain PHP error logs, detailed explanation of how to obtain PHP logs_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:46:011027browse

Detailed explanation of how to obtain PHP error logs, detailed explanation of how to obtain PHP logs

There are many ways to obtain error logs in PHP. Let me give you a brief introduction to how to obtain such logs. How to solve the problem, I will show you through the code below, I hope it can help you.

<&#63;php
error_reporting (0);
register_shutdown_function($err = 'my_error_handler') OR set_error_handler($err,E_ALL); // 同时注册两个函数.
// 函数参数错误
$original = unserialize ( array(423142,2134234));
// 不可对0除
echo 1/0;
// 不存在的数组索引
echo $_GET['aa'];
// 不存在的常量
PHP_E;
// 不存在的变量
echo $var;
// 函数参数错误
strlen(array(2434));
// 函数参数错误
md5(array(1));
// 用户自定义错误
trigger_error('safdds',E_USER_NOTICE);
// 不存在的函数 php中断
// tes();
// 不存在的方法 php中断
$ts = new afsd();
function my_error_handler($errno=0 ,$errstr=0 , $errfile=0 ,$errline=0){
 if($errno && $errfile){
  if(true){
   $earr = array();
   $earr['type'] = $errno;
   $earr['message'] = $errstr;
   $earr['file'] = $errfile;
   $earr['line'] = $errline;
  }
 }else{
  $earr = error_get_last();
 }
 echo '<pre class="brush:php;toolbar:false">';
 print_r($earr);
 return array();
}

For PHP errors, we must have a clear understanding of the types, when they are triggered, and what the return value is.

If we do not distinguish between warnings, errors, suggestions, prompts and other detailed types, then we can understand that there are two types of PHP error types:

One is forced interrupt program error. Such as error, method does not exist, etc. Insufficient memory, timeout

One type is a non-interrupting error. Such as warnings, prompt these

To obtain the interrupt program error, you need to use the register_shutdown_function function, register a memory function, and then obtain the error information. This method can only obtain the first error line. For example, when there are multiple non-existent functions, it can only obtain That mistake the first time. This actually has advantages, you fix one and it will move to the next one.

For non-interruptive errors, if you use:register_shutdown_function, it will become extremely difficult to operate. You can only get the first prompt or warning. At this time, we need set_error_handler. Every time the error level constants are touched, the error function will be called once. You can also get the error log inside. However, this function will inherit the error_get_last function, so you will find that error_get_last returns empty, all passed in by parameters.

Using the two functions together, you can get 99% of the errors in the program. For PHP programs, this is a lighter relief.

Try running the above example.

By combining code and text, we organize the methods of obtaining error logs in PHP. I hope it will be helpful to everyone who encounters such problems in future work.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1035667.htmlTechArticleDetailed explanation of how to obtain PHP error logs. Detailed explanation of how to obtain php logs. There are many ways to obtain error logs in PHP. Let me briefly introduce to you how to solve this kind of problem, below...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn