Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:01:49+00:00 2026-06-12T03:01:49+00:00

I’ve been doing some research on php error handling as well as exception handling.

  • 0

I’ve been doing some research on php error handling as well as exception handling.

For instance, to handle user errors, it is best to use set_error_handler() for user errors. Example code:

// Destinations
define("ADMIN_EMAIL", "nobody@stanford.edu"); 
define("LOG_FILE", "/my/home/errors.log");

// Destination types
define("DEST_EMAIL", "1");
define("DEST_LOGFILE", "3");

/**
* my_error_handler($errno, $errstr, $errfile, $errline)
*
* Author(s): thanosb, ddonahue
* Date: May 11, 2008
* 
* custom error handler
*
* Parameters:
*  $errno:   Error level
*  $errstr:  Error message
*  $errfile: File in which the error was raised
*  $errline: Line at which the error occurred
*/

function my_error_handler($errno, $errstr, $errfile, $errline)
{  
 switch ($errno) {
 case E_USER_ERROR:
  // Send an e-mail to the administrator
  error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);

  // Write the error to our log file
  error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
  break;

case E_USER_WARNING:
  // Write the error to our log file
  error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;

case E_USER_NOTICE:
  // Write the error to our log file
  error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;

default:
  // Write the error to our log file
  error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  break;
}

// Don't execute PHP's internal error handler
return TRUE;
}


// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler("my_error_handler");

Code found at http://www.stanford.edu/dept/its/communications/webservices/wiki/index.php/How_to_perform_error_handling_in_PHP.

Then for fatal errors:

register_shutdown_function('handleShutdown');

function handleShutdown() {
    $error = error_get_last();
    if($error !== NULL){
        $info = "[SHUTDOWN] file:".$error['file']." | ln:".$error['line']." |    msg:".$error['message'] .PHP_EOL;
        yourPrintOrMailFunction($info);
    }
    else{
        yourPrintOrMailFunction("SHUTDOWN");
    }
}

Code found at How do I catch a PHP Fatal Error.

From what I can tell, it appears that this will cover essentially all errors that can occur within a script (as a very general statement – it’s hard to predict all errors). When developing a class, thought, it seems that it is suggested to use exceptions to handle them internally.

My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.

In any case, are these functions attached to all files or set within a class?

Any help is appreciated.

Edit: I also meant to add, how would I stop a script if a certain error is encountered. Obviously, exit() or die() does the trick, but is there anything more appropriate to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:01:50+00:00Added an answer on June 12, 2026 at 3:01 am

    My question is whether or not these examples are generally considered appropriate to handle errors in a production setting, or if anything is blatantly incorrect or lacking.

    Yes it is. You just have to be sure that you catch ALL generated exception.
    I also want to remember you that if there is a synthax error in your scrip, or if your server is bad configured, php could output errors that are not handled because they are compile time errors.

    n any case, are these functions attached to all files or set within a class?

    Once you registered them (with set_error_handler) they are applied to every files that are called in your script apart from if one of your file redifine the handler.

    and to stop a script, exit is the good way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.