CodeIgniter Forums
Is this ok? Suppressing warnings with the @ symbol - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Lounge (https://forum.codeigniter.com/forumdisplay.php?fid=3)
+--- Thread: Is this ok? Suppressing warnings with the @ symbol (/showthread.php?tid=65915)

Pages: 1 2


Is this ok? Suppressing warnings with the @ symbol - PaulD - 08-09-2016

Hi,

Having just discovered get_meta_tags and dumping all my CURL functions a problem arose. During user testing some URLS (trimmed, properly validated, working and perfectly normal urls) were throwing an error. The function was called in an ajax call and the warning was displaying in the message field (normally used for 'Invalid URL' or 'Name too long' type error messages or 'Thanks, that site has been added' messages).

It seems that even with a TRY and CATCH error catching the warning was still coming through. It appears that the get_meta_tags, which returns FALSE on failure anyway, insists on throwing the warning for URL's it does not like for some reason or because the target server is simply not allowing that sort of request.

So, to stop it doing it, I just added an @ in front of the function call.

PHP Code:
$tags = @get_meta_tags($url); 

It works, but a few forums have been saying things like 'Don't use this' or 'Beware this approach as it can cause problems' etc.

This is not something I would normally do, but I cannot think of what else to do with this function. (Apart from going back to my much longer, slower but more predictable CURL functions again).

If anyone has an opinion or any ideas about this, they would be very much appreciated.

Best wishes,

Paul.


RE: Is this ok? Suppressing warnings with the @ symbol - InsiteFX - 08-09-2016

PHP.net

Error Control Operators


RE: Is this ok? Suppressing warnings with the @ symbol - Ivo Miranda - 08-09-2016

You should try to avoid it at all costs but if there is no easy way and you are sure about the possible errors there might be you could use it.

The STFU operator is there for some reason Smile


RE: Is this ok? Suppressing warnings with the @ symbol - ivantcholakov - 08-09-2016

If this function is quite chatty with warning messages I don't see anything wrong to suppress them with @-operator. Just add a comment - why did you put the operator there and why it should stay.


RE: Is this ok? Suppressing warnings with the @ symbol - PaulD - 08-09-2016

@ InsiteFX, Ivo Miranda and ivantcholakov

Much appreciated.

Thanks everyone! That makes me feel better about using it. Yes the function does seem 'chatty' (love that expression) and I had never heard of the 'stfu' operator (lol) but I think in this case I can use the @ control operator safely. Good idea about the comments though just in case anything peculiar starts to happen in the future and no errors are appearing to explain it.

Thank you again,

Paul.


RE: Is this ok? Suppressing warnings with the @ symbol - skunkbad - 08-09-2016

Some excellent advice, and a better solution than @:

http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning


RE: Is this ok? Suppressing warnings with the @ symbol - marksman - 08-09-2016

I use it to overcome huge compatibility issues from php 4 to php 5, but I'm trying to recode all those with compatibility issues


RE: Is this ok? Suppressing warnings with the @ symbol - skunkbad - 08-09-2016

(08-09-2016, 05:05 PM)marksman Wrote: I use it to overcome huge compatibility issues from php 4 to php 5, but I'm trying to recode all those with compatibility issues

Dang, to mention PHP4 ... reminds me of 10 years ago. Even then PHP4 was old, but still available on some hosts. Funny stuff.


RE: Is this ok? Suppressing warnings with the @ symbol - Narf - 08-10-2016

Avoiding the suppression operator usually means that, in order to prevent errors, you've otherwise checked for all possible error conditions and that in turn means good code, hence why it is the best practice.

There are a few cases where it's unavoidable (iconv() on user-input comes to mind) or not worth the effort, and this here may well be one of the latter.

You should still try to avoid error suppression in general though.


RE: Is this ok? Suppressing warnings with the @ symbol - awenna.machie - 08-10-2016

hahaha... sure is a good thing!