error_get_last() - php 错误处理日志函数
error_get_last()
(PHP 5 >= 5.2.0, PHP 7)
获取最后发生的错误
说明
error_get_last(void): array获取关于最后一个发生的错误的信息。
返回值
返回了一个关联数组,描述了最后错误的信息,以该错误的"type"、"message"、"file"和"line"为数组的键。如果该错误由 PHP 内置函数导致的,"message"会以该函数名开头。如果还没有错误则返回NULL
。
范例
Anerror_get_last()范例
以上例程的输出类似于:
Array ( [type] => 8 [message] => Undefined variable: a [file] => C:\WWW\index.php [line] => 2 )
参见
- Error 常量
- $php_errormsg变量
error_clear_last()
清除最近一次错误- $display_errors指令
- $html_errors指令
- $xmlrpc_errors指令
The error_get_last() function will give you the most recent error even when that error is a Fatal error. Example Usage:
[Editor's note: as of PHP 7.0.0 there is error_clear_last() to clear the most recent error.] To clear error_get_last(), or put it in a well defined state, you should use the code below. It works even when a custom error handler has been set.
If an error handler (see set_error_handler ) successfully handles an error then that error will not be reported by this function.
Function error_get_last() will return an error information even if the error is hidden because you've used character @, because of the "error_reporting" directive in the php.ini file, or because you've used function error_reporting(). Examples: Will display: array ( 'type' => 8, 'message' => 'Undefined variable: x', 'file' => 'test.php', 'line' => 4, ) Will display: array ( 'type' => 8, 'message' => 'Undefined variable: x', 'file' => 'test.php', 'line' => 4, )
Here is an easy way to add the human readable constant name for an error type: $e = error_get_last(); $e['type_name'] = array_search($e['type'], get_defined_constants()); echo($e['type_name']); // Example result for error type 15: E_COMPILE_ERROR
Beware that registing a shutdown function to catch errors won't work if other shutdown functions throw errors. In the above code, $last_error will contain the warning, becuase cleanupObjects() is called first.
It can't be completely reset, but you can "clear" it well enough for all practical purposes:
To simulate this function in a horrid way for php
To know if something happened between two statements one can of course use a special string with user_error() (in lieu of a built-in special reset mentioned by mail at mbaierl dot com): If your function returns true then you'll have to roll you own error_get_last functionality. (Shortly mentioned by dmgx dot michael at gmail dot com). To manual moderators: Re php.net/manual/add-note.php: Since i guess the above technically sorts under "References to other notes" i feel the need to defend myself with that i'm thinking it might show for usability where other's say it fails and no, i haven't got any other medium to reach the readers of the php manual notes. Also, you could have some examples of what notes you think is okay. Thanks for your moderation.
Like $php_errormsg, the return value of this function may not be updated if a user-defined error handler returns non-FALSE. Tested on PHP 5.2.6.
This is a simple debugging script for mail functions...
While mail at mbaierl dot com makes the point that this function isn't best for reporting the possible error condition of the most recently executed step, there are situations in which it is especially helpful to know the last error—regardless of when it occurred. As an example, imagine if you had some code that captured the output from dynamic pages, and cached it for faster delivery to subsequent visitors. A final sanity check would be to see if an error has occurred anywhere during the execution of the script. If there has been an error, we probably don't want to cache that page.
This function is pretty useless, as it can not be reset, so there is no way to know if the error really happened on the line before this function call.
If you have the need to check whether an error was a fatal error before PHP 5.2 (in my case, within an output buffer handler), you can use the following hack: This is, of course, language-dependent, so it wouldn't be good in widely-distributed code, but it may help in certain cases (or at least be the base of something that would work).
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)