Welcome Guest, Not a member yet? Register   Sign In
Is this ok? Suppressing warnings with the @ symbol
#1

(This post was last modified: 08-09-2016, 08:55 AM by PaulD.)

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.
Reply
#2

PHP.net

Error Control Operators
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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
Reply
#4

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.
Reply
#5

@ 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.
Reply
#6

Some excellent advice, and a better solution than @:

http://stackoverflow.com/questions/12417...-a-warning
Reply
#7

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
God Bless CI Contributors Smile
Reply
#8

(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.
Reply
#9

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.
Reply
#10

hahaha... sure is a good thing!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB