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
  • 30,738
  • 21
  • 105
  • 131
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • 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
  • 30,738
  • 21
  • 105
  • 131
fabrik
  • 14,094
  • 8
  • 55
  • 71
7

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
2

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

Chetan
  • 46,743
  • 31
  • 106
  • 145
1

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

SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61
  • @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 );