Skip to main content

Command Palette

Search for a command to run...

Suppressing errors in PHP

Catch fatal errors

Updated
1 min read
A
I am a web developer from Navi Mumbai. Mainly dealt with LAMP stack, now into Django and getting into Laravel and Cloud. Founder of nerul.in and gaali.in

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.");
}
36 views