I've seen code samples that use an @
before fopen
as in
$fh = @fopen($myFile, 'w');
What's the significance of this @
?
I've seen code samples that use an @
before fopen
as in
$fh = @fopen($myFile, 'w');
What's the significance of this @
?
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
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.