Content · Web Development · PHP

PHP Shutdown Functions and Fatal Error Logging

Shutdown functions run when a PHP script terminates and can inspect the last error. Not every termination guarantees cleanup, and error_get_last may report a nonfatal earlier.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Shutdown functions run when a PHP script terminates and can inspect the last error.

What It Does


Shutdown functions run when a PHP script terminates and can inspect the last error. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Start with the smallest meaningful working case.

<?php
set_error_handler(function (int $level, string $message): bool { echo 'Handled warning'; return true; });
trigger_error('Example warning', E_USER_WARNING);
restore_error_handler();

Example 2: Another Case


Examine the edge case and the observable result.

<?php
register_shutdown_function(function (): void { $last = error_get_last(); if ($last !== null) error_log('Shutdown code: ' . $last['type']); });
echo 'Shutdown handler registered';

Practical Example: Putting It to Use


Only treat selected error types as fatal and keep stack details out of responses.

<?php
register_shutdown_function(function (): void {
    $last = error_get_last();
    if ($last && in_array($last['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) {
        error_log('Fatal category: ' . $last['type']);
    }
});
echo 'Shutdown monitor configured';

Important Behavior and Edge Cases


Not every termination guarantees cleanup, and error_get_last may report a nonfatal earlier error.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Shutdown Functions and Fatal Error LoggingShutdown functions run when a PHP script terminates and can inspect the last error.
Related approachUse set_exception_handler() for uncaught exceptions and central logging for runtime events.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Shutdown Functions and Fatal Error LoggingNot every termination guarantees cleanup, and error_get_last may report a nonfatal earlier error.
Assuming a related mechanism has the same behaviorUse set_exception_handler() for uncaught exceptions and central logging for runtime events.

Key Takeaways


  • Shutdown functions run when a PHP script terminates and can inspect the last error.
  • Not every termination guarantees cleanup, and error_get_last may report a nonfatal earlier error.
  • Use set_exception_handler() for uncaught exceptions and central logging for runtime events.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.