Reputation: 2292
How may arrange that script execution stops upon notice/warning, without changing other behaviour, inc, the notice/warning messages, i.e. e.g. not throwing an exception.
Stop script execution upon notice/warning is a different question.
Upvotes: 1
Views: 739
Reputation: 41756
You can create a custom error handling function, like so:
<?php
// 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: exit('Im a user error.');
case E_USER_WARNING: exit('Im a user warning');
case E_USER_NOTICE:
printNotice($errno, $errstr, $errfile, $errline);
break;
default: exit('Unknown Error');
}
// don't execute PHP internal error handler
return true;
}
function printNotice($errno, $errstr, $errfile, $errline)
{
// use the following vars to output the original error
var_dump($errno, $errstr, $errfile, $errline);
exit;
}
Important are the constants: E_USER_NOTICE
and E_USER_WARNING
.
The function printNotice()
shows how you can print the original error,
so that it appears unmodified and then stops script execution with exit.
All the original error data (message, line number, etc.) is in the variables
$errno, $errstr, $errfile, $errline. PHP makes the error data automatically available at the registered errorhandler (here myErrorHandler).
In this example i'm forwarding all the parameters a second time, to printNotice() function, which could format the error differently or do what you like upon it.
And then register it with set_error_handler(), like so:
<?php
// register your error handler
set_error_handler('myErrorHandler');
In order to test the error handling you might use the function trigger_error():
trigger_error("This is a User Error", E_USER_ERROR);
trigger_error("This is a User Warning", E_USER_WARNING);
trigger_error("This is a User Warning", E_USER_NOTICE);
Upvotes: 1
Reputation: 631
Straight forward answer to your question is NO
you can't.
You have to create a custom error handler that stops execution.
As there is no way to do this outside.
Upvotes: 1