Error_get_last Fatal Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About try catch fatal error php Us Learn more about Stack Overflow the company Business Learn more about hiring php catch fatal error and continue developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
What Is Fatal Error In Php
Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do I catch a PHP
Php Error_get_last
Fatal Error up vote 411 down vote favorite 180 I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal (E\_ERROR) errors, such as calling a function that doesn't exist. Is there another way to catch these errors? I am trying to call mail() for all errors and am running PHP 5.2.3. php fatal-error share|improve this question edited Dec 22 '13 at 19:26 register_shutdown_function aksu 3,94451434 asked Nov 10 '08 at 6:42 too much php 41.8k26106123 add a comment| 17 Answers 17 active oldest votes up vote 504 down vote Log fatal errors using register_shutdown_function, which requires PHP 5.2+: register_shutdown_function( "fatal_handler" ); function fatal_handler() { $errfile = "unknown file"; $errstr = "shutdown"; $errno = E_CORE_ERROR; $errline = 0; $error = error_get_last(); if( $error !== NULL) { $errno = $error["type"]; $errfile = $error["file"]; $errline = $error["line"]; $errstr = $error["message"]; error_mail(format_error( $errno, $errstr, $errfile, $errline)); } } You will have to define the error_mail and format_error functions. For example: function format_error( $errno, $errstr, $errfile, $errline ) { $trace = print_r( debug_backtrace( false ), true ); $content = "
Item | Description |
---|---|
Error | $errstr |
Errno | $errno |
File | $errfile |
Line | $errline |
Trace | $trace |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about
Register_shutdown_function Fatal Error
Stack Overflow the company Business Learn more about hiring developers or posting ads with fatal error in php means us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is php throw fatal error a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Handle fatal errors in PHP using register_shutdown_function() up vote 21 down vote favorite 11 http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error According to the comment on this answer it is possible to catch Fatal Errors through a shutdown function which cannot be caught using set_error_handler(). However, I couldn't find out how to determine if the shutdown has occured due to a fatal error or due to the script reaching its end. Additionally, the debug backtrace functions seem to be defunct in the shutdown function, making it pretty worthless for logging the http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function stack trace where the Fatal Error occured. So my question is: what's the best way to react on Fatal Errors (especially undefined function calls) while keeping the ability to create a proper backtrace? php error-handling fatal-error share|improve this question asked Dec 10 '10 at 15:55 ThiefMaster♦ 188k38351465 add a comment| 4 Answers 4 active oldest votes up vote 39 down vote accepted This works for me: function shutdown() { $error = error_get_last(); if ($error['type'] === E_ERROR) { // fatal error has occured } } register_shutdown_function('shutdown'); spl_autoload_register('foo'); // throws a LogicException which is not caught, so triggers a E_ERROR However, you probably know it already, but just to make sure: you can't recover from a E_ERROR in any way. As for the backtrace, you can't... :( In most cases of a fatal error, especially Undefined function errors, you don't really need it. Pinpointing the file/line where it occured is enough. The backtrace is irrelevant in that case. share|improve this answer edited Dec 10 '10 at 16:22 answered Dec 10 '10 at 16:05 netcoder 42.8k984122 add a comment| up vote 5 down vote One way to distinguish between fatal error and proper application shutdown with the register_shutdown_function is to define a constant as the last line of your program, and
can’t cache FATAL Error because those are FATAL. This will be useful when you want to show specific well formatted error page rather than PHP Error, when your program http://www.xpertdeveloper.com/2013/01/catch-fatal-errors-in-php/ ends unexpectedly due to some fatal errors. We will use register_shutdown_function function to catch the fatal errors and format as we need. Keep in mind that register_shutdown_function will gets executed after your script execution ends either it is successful or ends with any error. So before performing any action in this function we need to check if really any fatal occurs or not. If occurs then we will show error page else it fatal error will continue to run as it is. So to get the details of the last error we will use error_get_last() function. Code function catch_fatal_error() { // Getting Last Error $last_error = error_get_last(); // Check if Last error is of type FATAL if(isset($last_error['type']) && $last_error['type']==E_ERROR) { // Fatal Error Occurs // Do whatever you want for FATAL Errors } } register_shutdown_function('catch_fatal_error');function catch_fatal_error() { // Getting Last Error $last_error = error_get_last(); catch fatal error // Check if Last error is of type FATAL if(isset($last_error['type']) && $last_error['type']==E_ERROR) { // Fatal Error Occurs // Do whatever you want for FATAL Errors } } register_shutdown_function('catch_fatal_error'); Share on Facebook Share on Twitter Related Posts PHP Error HandlingPHP Error ReportingEnable PHP Error logging using htaccessType Hinting in PHPAbstract in PHP Written by Avinash Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter. View all posts by: Avinash MM Actually Good point is highlighted that register_shutdown_function will be executed even if your script ends successfully or with error. So if you have set error logging then you must check for last error. Pingback: PHP catch fatal errors | Wiki 42 Pingback: 30+ Links to PHP Training Materials, News about Zend Optimizer+, MySQL 5.6 Release and More | Zfort Group Blog gaurish hi Avinash, can you please give any example , how this works…!!! thanks, Gaurish http://www.xpertdeveloper.com Avinash Which portion you are not able to understand? Recent Posts 3 Benefits to Test Extensively All Along Mobile App