0

I'm trying to setup error handling for the first time.
It does actually work and report errors if there is one, but for some reason it always shows errors of 'missing arguments' for the error handling function itself. Please note my error handling function is in a separate file and is included to the index page, I'm not sure if that's the problem :S

Here is my error handling function

function errorHandler($errno, $errstr, $error_file, $error_line) {

  if(isset($errstr)) {
    # There is an error, display it
    echo $errno." - ".$errstr." in ".$error_file." at line ".$error_line."<br>";
  } else {
    # There isn't any error, do nothing
    return false;
  }

}

// We must tell PHP to use the above error handler.
set_error_handler("errorHanlder");

Here is the index page

<!-- # Error Handler -->
<? if(errorHandler()) { ?>
<section id="error-handler">
  <?=errorHandler();?>
</section>
<? } ?>

Here is the result in the browser (bear in mind there is no php error, so this error handler shouldn't be outputting anything - this is what I can't understand

2 - Missing argument 1 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 2 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 3 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 4 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10

Any idea's why PHP is reporting the missing arguments?

1 Answer 1

7

You are calling the function with ZERO argument...

<?=errorHandler();?>

Why do you need to call it anyway?

And there are several typos in your code: replace "Hanlder" with "Handler".

There's no need to do that:

if(isset($errstr)) {

Your error-handling function is automatically called when there is an error (and ONLY in that case!). $errstr is a parameter of this function, it is always set when the function gets executed.

New code:

function errorHandler($errno, $errstr, $error_file, $error_line) {
    # There is an error, display it
    echo "<section id='error-handler'>$errno - $errstr in $error_file at line $error_line</section><br>";
}

// We must tell PHP to use the above error handler.
set_error_handler("errorHandler");
Sign up to request clarification or add additional context in comments.

5 Comments

I'm trying to understand how this all works. If I pass arguments of $errno, $errstr, $errfile, $errline to errorHandler then it outputs the same. Fixed typos
Jocelyn, thanks for your answer. I understand the logic of how error handlers work now.. I thought for some reason I had to call the function INCASE it had an error otherwise it wouldn't show errors when there were errors.. Now I've sorted it, but how can I tell PHP where to display the error?
I see your change to echo the <section>, which makes sense. THanks for your help buddy!
Don't forget to fix the typo in set_error_handler("errorHanlder");. It should be set_error_handler("errorHandler");
but how can I tell PHP where to display the error? <- handle each error individually if you want to show it at an specific place of the page, the error handler is only there as a last resort call and should only be used as such.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.