Sunday, 17 September 2017

Suppress error with @ operator in PHP



In your opinion, is it ever valid to use the @ operator to suppress an error/warning in PHP whereas you may be handling the error?



If so, in what circumstances would you use this?



Code examples are welcome.




Edit: Note to repliers. I'm not looking to turn error reporting off, but, for example, common practice is to use



@fopen($file);


and then check afterwards... but you can get rid of the @ by doing



if (file_exists($file))
{
fopen($file);

}
else
{
die('File not found');
}


or similar.



I guess the question is - is there anywhere that @ HAS to be used to supress an error, that CANNOT be handled in any other manner?



Answer



I would suppress the error and handle it. Otherwise you may have a TOCTOU issue (Time-of-check, time-of-use. For example a file may get deleted after file_exists returns true, but before fopen).



But I wouldn't just suppress errors to make them go away. These better be visible.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...