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

  • SEARCH
  • Home
  • 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 8221497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:56:34+00:00 2026-06-07T13:56:34+00:00

I must be missing something in my understanding of handling PHP errors, specifically suppressing

  • 0

I must be missing something in my understanding of handling PHP errors, specifically suppressing their output. When a fatal error occurs, I expect my shutdown handler function to process it gracefully and terminate script execution. This works as expected. However, I can’t seem to prevent PHP from outputting the information about the fatal error.

My php.ini file contains the following directives:

error_reporting = E_ALL | E_STRICT
display_errors = Off

I set error_reporting to report everything and I use a custom error handler to throw exceptions. My expectation is that display_errors = Off will prevent ANY error messages from being displayed.

Anyway, when a fatal error occurs the custom error handler is bypassed (because script execution stops immediately) and the shutdown handler executes.

Now, on to my simplified code:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'Off');

function shutdown_handler()
{
  $err = error_get_last();
  $fatal = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR);
  if ($err && in_array($err['type'], $fatal)) {
    echo "\n\ntest fatal error output\n\n";
  }
  exit();
}

register_shutdown_function('shutdown_handler');

To test it I generate an “Allowed memory size exhausted” fatal error like so:

// max out available memory
$data = '';
while(true) {
  $data .= str_repeat('#', PHP_INT_MAX);
}

Because i have display_errors = Off I expect this to only produce the following output (as per the shutdown handler):

test fatal error output

But instead I continue to receive:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 2147483648 bytes) in /home/daniel/mydev/php/test0.php on line 24
PHP Stack trace:
PHP   1. {main}() /home/daniel/mydev/php/test0.php:0
PHP   2. str_repeat() /home/daniel/mydev/php/test0.php:24

test fatal error output

What am I missing that will prevent this error trace from outputting?


CONCLUSION

It appears, as @Cthos sagely noted, that “E_ERROR and display_errors don’t play nicely together.”

This is also the case for E_PARSE (and I assume E_CORE_ERROR/E_COMPILE_ERROR, but I didn’t break my PHP installation to test it). I suppose it makes sense that PHP would force the error traceback into STDOUT in these cases because if it didn’t you might never know if/why things were going wrong.

So a solution in this case would be:

  1. As @Cthos suggested, silence E_ERROR notifications in php.ini: error_reporting = (E_ALL & ~ E_ERROR) or at runtime using error_reporting(E_ALL & ~ E_ERROR);
  2. Update the shutdown handler to check if the last error was of type E_ERROR and perform the appropriate actions if so.

As for the other fatals like E_PARSE, E_CORE_ERROR, etc. you just have to make sure your code is correct and that your PHP works. If you try to silence E_PARSE errors and handle them in your shutdown function it won’t work because parse errors prevent PHP from ever getting that far.

So, an updated/working shutdown handler looks like this:

error_reporting(E_ALL & ~ E_ERROR);

function shutdown_handler()
{
  $err = error_get_last();
  if ($err && $err['type'] == E_ERROR) {
    $msg = 'PHP Fatal Error: '.$err['message'].' in '.$err['file'].
      ' on line '.$err['line'];
    echo $msg, PHP_EOL;
  }
  exit();
}
  • 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-07T13:56:37+00:00Added an answer on June 7, 2026 at 1:56 pm

    The unwanted error information is due to the error logging configuration (log_errors and error_log), and is completely independent of the display_errors configuration. The default php.ini configuration logs error details to stderr when running scripts from the command-line. Under Apache, they show up in /etc/httpd/logs/error_log.

    Check out the php_error_cb function in main.c of the PHP source. In particular, line 1056 makes it clear that any messages of the format “PHP %s: %s in %s on line %d” are due to the error logging configuration (error information due to display_errors will not have the “PHP: ” prefix).

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

Sidebar

Related Questions

I must be missing something obvious, but still... I get MySQL ERROR 1064 (42000)
There must be something Im missing in my understanding of how .NET's authentication/authorization and
I must be missing something here... I have the following code and output. Can
I must be missing something. I have a simple jquery autocomplete: $(input#txtApplicationName).autocomplete({ source: ApplicationProcess.php,
Must be missing something really obvious here but how do you change the size
I must be missing something really obvious. I'm quite new to C# but have
I must be missing something very simple, but can't find the answer to this.
I must be missing something obvious here... I can't get .change() to fire on
I must be missing something obvious but I'm not sure what. I've created a
I must be missing something obvious about how the various Scanner methods work but

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.