Suppressing errors in PHP

Catch fatal errors

It looks like fatal errors can't be suppressed using the @ sign anymore in my PHP setup using MAMP.

I had this all along in PHP 7.4:
@session_destroy(); # Throws an error in PHP7, so hence the @

When I switched to PHP 8.0.0 the fatal error still shows up.

Fatal errors should be tested and handled during the development phase itself, but in my case, I have a custom session handler where session_destroy() requires a custom session function and I can't figure out what the issue is. So I suppressed the session_destroy() call since it's called in the logout page which is normally the last action of a logged in user - this isn't mail or social media. Fixing this custom session handler is for another day but until then, I'll disregard the fatal error.

Since PHP 7, there's a way to handle errors (not exceptions).

try
{
    session_destroy();
}
catch (Error $e) # instead of Exception $e 
{
   die("Logged out.");
}

No Comments Yet