tags:

views:

44

answers:

3

I've seen code samples that use an @ before fopen as in

$fh = @fopen($myFile, 'w');

What's the significance of this @?

+5  A: 

It suppresses errors an expression may display.

You can read more about it here.

Example:

file_get_contents('file_does_not_exist'); //this shows an error

@file_get_contents('file_does_not_exist'); //this does not
Tim Cooper
I used this a great deal with mysql function calls.
James Black
Thanks, is it good or bad practice then?
brett
It's generally not a good idea. Instead, you should use a function like `is_writable` to test if a file is writable.
Tim Cooper
For production applications, it's generally a bad idea - suppressing errors is a performance hit.
Amber
@Dav a performance hit is a wild superstition. But using @ is still bad as it will leave programmer without critical info.
Col. Shrapnel
+1  A: 

Its PHP's error control char.

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

More here.

codaddict
A: 

Using @ is always bad practice. It will suppress an error message if occurrs, while error messages are extremely useful for the programmer and suppressing it is suicide. PHP's fate is to be used mostly not by programmers but by casual users who have no clue. You've got this code from one of that latter kind. So, better to get rid of all @, so, you'll be able to see what happened and correct a mistake.
Note that every error message has it's particular meaning and explain what the problem is. For example there can be filesystem permissions problem or PHP OPEN_BASEDIR setting to prevent file from open. So, an error message will tell you what to do. Error messages is good and @ is evil.

Col. Shrapnel