rubo77 rubo77 - 7 days ago 5
PHP Question

Counter how many errors were triggered in PHP

If I have to call to a third-party legacy function that doesn't allow proper error checking,
is there a global counter on how many error-notices were already triggered by a php script?

I would like to detect in my script if there were errors/warnings/notices during a function call triggered and react accordingly in my script.

As a workaround, I would go for counting the number of rows in the apache errorlog before and after the function call, but that doesn't work:

<?php
$errorlogsize_before=filesize(DEBUGMODE_LOGFILE);
third_party_function();
$errorlogsize_after=filesize(DEBUGMODE_LOGFILE);
echo "Logsize before:".$errorlogsize_before."; Logsize after:".$errorlogsize_after."


They display the same filesize.

Answer

I'll assume your use case is a call to a third-party legacy function that doesn't allow proper error checking. In that case, you can change the error handler temporarily with set_error_handler() and restore it afterwards with restore_error_handler(). Other alternatives like error_get_last() discard all errors but the last one.

Comments