views:

50

answers:

2

Hello all,

Is there a way I can count the number of errors/notices/warnings that a script has come across whilst executing?

I wish to do something like this:

Warnings: 125
Notices: 234
..etc

Thanks

A: 

You could use set_error_handler() to define a custom error handler which increments a global counter as well as logging/displaying the error.

Tom Haigh
"Fastest gun" goes to you, sir.
TML
+4  A: 
$warn = $notice = 0;  
function f() { 
  global $warn, $notice; 
  $argv = func_get_args(); 
  switch($argv[0]) { 
    case E_WARNING: $warn++; break; 
    case E_NOTICE: $notice++; break; 
  }
}
set_error_handler('f', E_ALL);

Expand as necessary :)

TML
You might want to return false, then PHP's own error handler will still run
Tom Haigh
Thank you. I have a 3500 lines php script and with your help, I have found I have 3,204,367 notices. I can't believe this script is that bad. But any chance that function may have counted wrong! :P - I placed that at the top and echoed out warn and notice at the bottom. Is this ok?
Abs
Tom makes a good point, I just assumed you wanted to suppress all other error handling.3 million certainly does seem high, but I can't think of what I might have missed. Perhaps you could join the ##PHP channel on the freenode irc network and pastebin your script, I'm sure one of us there could take a deeper look with you.
TML
3 million notices is completely believable, if you've got a loop that executes several thousand times that throws a number of notices.
Frank Farmer
If you really want to debug your script (in DEV) go to `E_STRICT` mode.
Toto