24

Basically, I've seen people using @ before their function calls, not for every function, but for some kind of extension functions like file_get_contents(), mysql_connect() and so on.

And yes, the question is: For what purpose are there these @s before function calls?

Or in other words, what is the difference between @file_get_contents() and file_get_contents()?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
tomsseisums
  • 12,335
  • 19
  • 75
  • 138
  • 1
    See [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Oct 21 '10 at 06:53
  • possible duplicate of [What does @ mean in PHP?](http://stackoverflow.com/questions/3621215/what-does-mean-in-php) – Gordon Oct 21 '10 at 06:53

5 Answers5

41

@ is an error control operator. Basically it's suppressing errors.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
fabrik
  • 13,237
  • 8
  • 54
  • 69
7

It's the PHP's error control operator used to suppress any error generated by the function call.

codaddict
  • 410,890
  • 80
  • 476
  • 515
1

The @ symbol in front of a function prevents errors from being displayed when the function is called.

Chetan
  • 41,886
  • 27
  • 101
  • 142
1

@function doesn't show any error messages on its HTML output, while a regular function call will.

SkypeMeSM
  • 2,876
  • 7
  • 40
  • 59
  • @fabrik the same reason as yours. But while yours is ambiguous (just "output"), this one is cleanly states HTML output, which, of course, is a nonsense. Error control has nothing to do with program's output. – Your Common Sense Oct 21 '10 at 06:50
  • @fabrik: Honestly I do not understand if you are mocking or genuinely asking. Either way I do not care. @Col. Shrapnel: Requesting you to type out your definition of error control for all of us to gain more insight on the subject. I agree that my answer lacks any depth, since I just said what I understood it to be :) Thanks. – SkypeMeSM Oct 21 '10 at 07:11
  • 2
    he was talking not to you. But to person who downvoted. Got it now? Error control consists of 2 parts: standard error output *destination* and *level* of error. Both `@` operator and `error_reporting` setting are control *level* of error. (@ sets it to 0) While `log_errors` and `display_errors` settings responsible for the destination. Latter one is responsible for what you have said: HTML output. While @ has nothing to do with it, id should be never used for that purpose – Your Common Sense Oct 21 '10 at 08:47
0

I have similar doubt about @ used in front of functions. To avoid this I made some verification before the function call. My example is:

if ( is_file($filename) ) $timestamp = filemtime( $filename );