How to display php error like Codeigniter?

25

I am working on PHP online IDE using core PHP. How to display PHP error like CodeIgniter when user tests php code?

Screenshot

603

Answer

Solution:

I can't say for exactly like codeignitor because I have never used it, but:

Method 1

You can edit php.ini

; String to output before an error message.
error_prepend_string = "<font color=ff0000><b>An Error Occured</b></font><i> Found an error as follows:</i><br><code>"

; String to output after an error message.
error_append_string = "</code>"

You may edit the HTML and use some ways(like js,though,I don't know if it is a good idea) to make it look better

Method 2

Use a custom handler:

function myErrorHandler($errno, $errstr, $errfile, $errline) {

$sev = "";

$errorType = array (
           E_ERROR            => 'ERROR',
           E_WARNING        => 'WARNING',
           E_PARSE          => 'PARSING ERROR',
           E_NOTICE         => 'NOTICE',
           E_CORE_ERROR     => 'CORE ERROR',
           E_CORE_WARNING   => 'CORE WARNING',
           E_COMPILE_ERROR  => 'COMPILE ERROR',
           E_COMPILE_WARNING => 'COMPILE WARNING',
           E_USER_ERROR     => 'USER ERROR',
           E_USER_WARNING   => 'USER WARNING',
           E_USER_NOTICE    => 'USER NOTICE',
           E_STRICT         => 'STRICT NOTICE',
           E_RECOVERABLE_ERROR  => 'RECOVERABLE ERROR'
           );

if(!isset($errorType($errno))
{ 
    $sev = "Unknown";
}
else
{
    $sev = $errorType[$errno];
}
echo "<div style= 'border-style: solid;border-color: #98bf21;'>";
echo "<b>A PHP Error was enccounter</b>";
echo "<li>Severity: $sev";
echo "<li>Message: $errstr";
echo "<li>Filename: $errfile";
echo "<li>Line NUmber: $errline";
echo "</div>";
/* Don't execute PHP internal error handler */
return true;
}

function shutdown() {

$isError = false;

if ($error = error_get_last()){
switch($error['type']){
    case E_ERROR:
    case E_CORE_ERROR:
    case E_COMPILE_ERROR:
    case E_USER_ERROR:
        $isError = true;
        break;
    }
}

if ($isError){
    myErrorHandler($error['type'],$error['message'],$error['file'],$error['file'])
    }
}

And set it as

set_error_handler("myErrorHandler");
register_shutdown_function('shutdown');

Check it with this:

un_defined_function(100);

More Info

People are also looking for solutions to the problem: mysql - Something is wrong with my custom php function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.