Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将php错误消息读入字符串变量_Php - Fatal编程技术网

如何将php错误消息读入字符串变量

如何将php错误消息读入字符串变量,php,Php,我正在做一个小程序,当错误发生时需要读取错误会议 例如,一个操作触发了一个错误,php日志将其记录为: PHP Fatal error: Smarty error: [in /mnt/n2my_web/templates/ja_JP/mail/reservation_create.t.txt line 16]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2338) in /mnt/n2my_web/

我正在做一个小程序,当错误发生时需要读取错误会议

例如,一个操作触发了一个错误,php日志将其记录为:

PHP Fatal error:  Smarty error: [in /mnt/n2my_web/templates/ja_JP/mail/reservation_create.t.txt line 16]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2338) in /mnt/n2my_web/lib/Smarty/Smarty.class.php on line 1092
我通过设置ini_set('display_errors','1')知道可以打印错误消息。但我需要阅读它,以便格式化它

我用什么方法可以做到这一点?如有任何答复,我们将不胜感激。:)

使用这个:,为了处理错误,我建议您只使用自定义异常和错误处理程序


实际上,访问致命错误周围的任何错误信息的唯一方法是使用
register\u shutdown\u function()
尝试捕获这些错误,并在脚本终止之前处理它们。这不是100%可靠

register_shutdown_function(function() {
   $last_error = error_get_last();
   if(!is_null($last_error) && $last_error['type'] === E_ERROR) {
       /*
       Do something with $last_error info.
       $last_error will contain array with keys as follows:
       ['type']['message']['file']['line']
       */
   }
});

你想知道一般错误还是致命错误的具体情况,这是一个完全不同的问题,不能像正常错误一样处理。嗨,迈克,实际上这个项目中的错误总是smarty错误,也就是说所有错误都应该是php致命错误。值得注意的是
error\u get\u last()
对于比
E\u警告更严重的事件没有任何好处,因为执行会在该点停止。嗨,Egg,谢谢你的建议。至于这个问题,正如Sammitch所说,应该考虑终止点。我想是我的快车引起了混乱,谢谢。嗨,迈克,我的脚本在调用smarty函数时终止了。所以在调用smarty函数之前,我把register\u shutdown\u function()放在了一行,它似乎不起作用。然后我把它放在我自己函数的开头,也不起作用。在哪里注册应该是一个问题?迈克,你的答案是对的,但我想除非ini_设置('display_errors','1');已设置,否则将不返回错误消息。我不确定,在我的实践中,如果没有设置display_errors,我就无法从error_get_last()获取消息;