Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to suppress errors in PHP

I've got some variables that may or may not be defined (an $isLoggedIn boolean) and I'm trying to clean out the error messages. I was wondering if there was any reason I shouldn't use the error suppressor operator:

if (@$isLoggedIn)

Or if I should instead check the existance of the variable first:

if (isset($isLoggedIn)and$isLoggedIn)

In a production environment is there any drawback/benefit to either approach in particular? The functionality of both statements is identical and there are no problems caused by this var being undefined. It shouldn't be logged as an error though.

like image 648
Ben Brocka Avatar asked Jan 20 '23 00:01

Ben Brocka


2 Answers

In my experience, you should never suppress errors on an individual level. In your production environment set error reporting to 0 and display errors to off. Having individual supressions like this is shooting yourself in the foot when you come back to fix an error in 6 months, forget it's there and can't work out why no errors are showing up but something is clearly broken.

In you particular case, just set $isLoggedIn to false then override that value at the location you are currently instantiating it (I'm assuming code structure here).

like image 191
Endophage Avatar answered Jan 21 '23 15:01

Endophage


If you're using a variable, it should exist. Your code shouldn't suppress errors, you should fix them or deal with them.

like image 36
Incognito Avatar answered Jan 21 '23 14:01

Incognito