Forum Moderators: coopster

Message Too Old, No Replies

Using @ to suppress errors

         

johnblack

10:07 pm on May 26, 2011 (gmt 0)



Hi all,

I use the fsockopen function to test whether a valid url has been entered or not as follows

$fp = @fsockopen($testurl, 80, $errno, $errstr, 10);

if (!$fp) {

// Generate error code


My question is about the use of @ to suppress the php errors that are generated by using fsockopen with an invalid url.

Is it 'best practice' to use the @ in this situation? I test the result of the fsockopen call by checking $fp afterwards, is that OK?

Cheers JB

Matthew1980

12:58 pm on May 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there John Black,

I think that using @ to suppress errors is bad practice, but, even on the php manual, there are instances that using @ is actually advocated; off the top of my head I think this may actually be case in point.

But as a rule of thumb - don't suppress those errors, bad programming really.

Cheers,
MRb

coopster

1:36 pm on May 27, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I try to avoid using the Error Control Operator [php.net] if possible too. However, there was one case and I believe it was also a stream application I was writing that would fail if the errors were not suppressed. That was a long time ago and I want to say it was an FTP stream.

But once again, I want to say that I agree with Matthew1980 here. If you can avoid using error suppression and log errors instead you will find your application much better served as you may continue to refine it based on the errors that will no longer be suppressed, including hack attempts, etc.