0

I am developing an website but when am escaping then inserting records into database, I am using try catch.

I want to create a global error array which I can add to any exception message if the try fails and catching an exception.

This array would later be used on my php page to show the error that occurred to the user.

How would I go about creating this array?

Thanks,

1 Answer 1

1

You can create a generic error handler like this one:

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
    // This error code is not included in error_reporting
    return;
    }

    switch ($errno) {
        case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

        case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

        case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

        default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

// set to the user defined error handler

set_error_handler("myErrorHandler");
Sign up to request clarification or add additional context in comments.

4 Comments

Seems unrelated to the question. This is not creating an array.
I think this better answers the question as it pens up the original poster to a better way of handling errors than a global array.
yes this is a function that I will need to place in a class to access. I am in need for something like $_SESSION which is accessible from everywhere.
If you use something like that in you base config file or base function file then you have access everywhere. Also you can create similar function to call with different level if you run in devel or in production mode your app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.