0

I'm having difficulty debugging my PHP Application.

When I include my php file, no exception is thrown. I tried using set_error_handler, which does fire, but error_get_last() returns null.

set_error_handler(function() {
   echo 'Here is your error!';
   var_dump(error_get_last());
   echo 'Sike!';
});
try {
    include('viewer.php');
} catch (Exception $e) {
    echo 'I really ought to tell you something went wrong here. But I won\'t';
}
restore_error_handler();
exit();

Output:

Here is your error!NULL Sike!

Is there some third way I can find out what actually went wrong?

Thomas Harris
  • 341
  • 1
  • 10
  • I don't understand. Are you asking how to display PHP errors? See https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display - `include` does not throw exceptions. – Xatenev Feb 03 '19 at 20:06
  • `error_get_last` won't retrieve errors that were caught by a custom error handler. You can simply use the arguments that will be passed to your callback. Check [`set_error_handler`'s docs](http://www.php.net/set_error_handler). – Jeto Feb 03 '19 at 20:15

1 Answers1

0

from php docs try this

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting, so let it fall
        // through to the standard PHP error handler
        return false;
    }

    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_error_handler("myErrorHandler");
Ali Ghalambaz
  • 390
  • 2
  • 6