How to make php output to E_NOTICE?

Usually the php script continues to work after E_NOTICE, is there a way to raise this to a fatal error in the context of the function, that is, I only need to exit notifications in my functions, but not in the main php functions, globally.

+5
source share
2 answers

You can create your own error handler to catch E_NOTICEs.

This has not been verified, but should go in the right direction:

function myErrorHandler($errno, $errstr, $errfile, $errline)
 {
  if ($errno == E_USER_NOTICE)
   die ("Fatal notice");
  else
   return false; // Leave everything else to PHP error handling

 }

then install it as a new custom error handler using set_error_handler()when entering your function and restore the PHP error handler when it exits:

function some_function()
{

// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");

... do your stuff ....

// Restore old error handler
set_error_handler($old_error_handler);

}
+9
source

You use a special error handler using set_error_handler()

<?php

    function myErrorHandler($errno, $errstr, $errfile, $errline) {
        if ($errno == E_USER_NOTICE) {
            die("Died on user notice!! Error: {$errstr} on {$errfile}:{$errline}");
        }
        return false; //Will trigger PHP default handler if reaches this point.
    }

    set_error_handler('myErrorHandler');

    trigger_error('This is a E_USER_NOTICE level error.');
    echo "This will never be executed.";


?>

+2

All Articles