SteppingHat
SteppingHat

Reputation: 1362

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!

Upvotes: 1

Views: 1694

Answers (2)

shybovycha
shybovycha

Reputation: 12235

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 />";
        }
    }
?>

Upvotes: 2

Matt Whitehead
Matt Whitehead

Reputation: 1801

I don't know about putting them all at the bottom of the page but what you could do is send all of the error messages to an error file like this:

error_reporting(E_ALL);
ini_set('display_errors', 'off');
ini_set('log_errors', 'on');
ini_set('error_log', 'path/to/error/file.log');

Also, while you're doing your coding and testing, you can keep the file open in your IDE and check it after each test to see if any new errors occurred.

Upvotes: 0

Related Questions