Search code examples
phperror-reporting

Printing errors at the bottom of the page instead of the top?


While developing my website, I have error reporting enabled. But every time an error is generated, it gets generated on the top of the page, before the opening tag. This breaks the document sometimes and changes many aspects of the website such as performance and the way in elements are displayed.

Is there any way to collect all of the errors and display them all at the bottom of the page?

Thanks heaps!


Solution

  • You could create your own error handler and collect all the errors into the file, for instance. Or into the array and then show all the errors at the bottom of the page, as you requested.

    Here's how it should go:

    <?php
    // At the top of your PHP code
    
    class MyError
    {
        protected static $collected = array();
    
        public static function getCollectedErrors()
        {
          return self::$collected;
        }
    
        protected static function addError($key, $error)
        {
          if (!isset(self::$collected[$key]))
            self::$collected[$key] = array();
    
          self::$collected[$key][] = $error;
        }
    
        // CATCHABLE ERRORS
        public static function captureNormal( $number, $message, $file, $line )
        {
            // Insert all in one table
            $error = array( 'type' => $number, 'message' => $message, 'file' => $file, 'line' => $line );
            // Display content $error variable
            self::addError('error', $message . " at " . $file . ':' . $line);
        }
    
        public static function captureException( $exception )
        {
            // Display content $exception variable
            self::addError('exception', $exception);
        }
    
        // UNCATCHABLE ERRORS
        public static function captureShutdown( )
        {
            $error = error_get_last( );
            if( $error ) {
                ## IF YOU WANT TO CLEAR ALL BUFFER, UNCOMMENT NEXT LINE:
                # ob_end_clean( );
    
                // Display content $error variable
                self::addError('shutdown', $error);
            } else { self::addError('shutdown', '<none>'); return true; }
        }
    }
    
    set_error_handler( array( 'MyError', 'captureNormal' ) );
    set_exception_handler( array( 'MyError', 'captureException' ) );
    register_shutdown_function( array( 'MyError', 'captureShutdown' ) );
    ?>
    

    And then, you could get the access to all the errors, by category using this:

    Error::getCollectedErrors();
    

    UPD: To display the errors at the bottom of the page, add this code to the place you want to output the errors:

    <?php
        $errors = MyError::getCollectedErrors();
    
        foreach ($errors as $category => $items) {
            echo "<strong>" . $category . ":</strong><br />";
    
            foreach ($items as $error) {
                echo $error . "<br />";
            }
        }
    ?>